Search in sources :

Example 11 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project perun by CESNET.

the class AttributesManagerImpl method createAttribute.

public AttributeDefinition createAttribute(PerunSession sess, AttributeDefinition attribute) throws InternalErrorException, AttributeExistsException {
    if (!attribute.getFriendlyName().matches(AttributesManager.ATTRIBUTES_REGEXP)) {
        throw new InternalErrorException(new IllegalArgumentException("Wrong attribute name " + attribute.getFriendlyName() + ", attribute name must match " + AttributesManager.ATTRIBUTES_REGEXP));
    }
    try {
        int attributeId = Utils.getNewId(jdbc, "attr_names_id_seq");
        jdbc.update("insert into attr_names (id, attr_name, type, dsc, namespace, friendly_name, display_name, created_by, created_at, modified_by, modified_at, created_by_uid, modified_by_uid) " + "values (?,?,?,?,?,?,?,?," + Compatibility.getSysdate() + ",?," + Compatibility.getSysdate() + ",?,?)", attributeId, attribute.getName(), attribute.getType(), attribute.getDescription(), attribute.getNamespace(), attribute.getFriendlyName(), attribute.getDisplayName(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), sess.getPerunPrincipal().getUserId());
        attribute.setId(attributeId);
        log.info("Attribute created: {}", attribute);
        return attribute;
    } catch (DataIntegrityViolationException e) {
        throw new AttributeExistsException("Attribute " + attribute.getName() + " already exists", e);
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : AttributeExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeExistsException) ConsistencyErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 12 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project perun by CESNET.

the class PublicationManagerBlImpl method deletePublication.

@Override
public void deletePublication(PerunSession sess, Publication publication) throws CabinetException, InternalErrorException {
    try {
        // delete authors
        for (Authorship a : getAuthorshipManagerBl().getAuthorshipsByPublicationId(publication.getId())) {
            getAuthorshipManagerBl().deleteAuthorship(sess, a);
        }
        // delete thanks
        for (Thanks t : getThanksManagerBl().getThanksByPublicationId(publication.getId())) {
            getThanksManagerBl().deleteThanks(sess, t);
        }
        // delete publication
        if (AuthzResolver.isAuthorized(sess, Role.PERUNADMIN)) {
            // only perun admin can actually delete publication
            getPublicationManagerDao().deletePublication(publication);
            log.debug("{} deleted.", publication);
        }
    // publications without authors are: "to be deleted by perun admin"
    } catch (DataIntegrityViolationException ex) {
        throw new CabinetException("Can't delete publication with Authors or Thanks. Please remove them first in order to delete publication.", ErrorCodes.PUBLICATION_HAS_AUTHORS_OR_THANKS);
    } catch (PerunException ex) {
        throw new CabinetException(ErrorCodes.PERUN_EXCEPTION, ex);
    }
}
Also used : Authorship(cz.metacentrum.perun.cabinet.model.Authorship) Thanks(cz.metacentrum.perun.cabinet.model.Thanks) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 13 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project spring-boot by spring-projects.

the class JooqAutoConfigurationTests method jooqWithoutTx.

@Test
public void jooqWithoutTx() throws Exception {
    registerAndRefresh(JooqDataSourceConfiguration.class, JooqAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    assertThat(getBeanNames(PlatformTransactionManager.class)).isEqualTo(NO_BEANS);
    assertThat(getBeanNames(SpringTransactionProvider.class)).isEqualTo(NO_BEANS);
    DSLContext dsl = this.context.getBean(DSLContext.class);
    dsl.execute("create table jooqtest (name varchar(255) primary key);");
    dsl.transaction(new AssertFetch(dsl, "select count(*) as total from jooqtest;", "0"));
    dsl.transaction(new ExecuteSql(dsl, "insert into jooqtest (name) values ('foo');"));
    dsl.transaction(new AssertFetch(dsl, "select count(*) as total from jooqtest;", "1"));
    try {
        dsl.transaction(new ExecuteSql(dsl, "insert into jooqtest (name) values ('bar');", "insert into jooqtest (name) values ('foo');"));
        fail("An DataIntegrityViolationException should have been thrown.");
    } catch (DataIntegrityViolationException ex) {
    // Ignore
    }
    dsl.transaction(new AssertFetch(dsl, "select count(*) as total from jooqtest;", "2"));
}
Also used : DSLContext(org.jooq.DSLContext) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 14 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project spring-boot by spring-projects.

the class JooqAutoConfigurationTests method jooqWithTx.

@Test
public void jooqWithTx() throws Exception {
    registerAndRefresh(JooqDataSourceConfiguration.class, PropertyPlaceholderAutoConfiguration.class, TxManagerConfiguration.class, JooqAutoConfiguration.class);
    this.context.getBean(PlatformTransactionManager.class);
    DSLContext dsl = this.context.getBean(DSLContext.class);
    assertThat(dsl.configuration().dialect()).isEqualTo(SQLDialect.H2);
    dsl.execute("create table jooqtest_tx (name varchar(255) primary key);");
    dsl.transaction(new AssertFetch(dsl, "select count(*) as total from jooqtest_tx;", "0"));
    dsl.transaction(new ExecuteSql(dsl, "insert into jooqtest_tx (name) values ('foo');"));
    dsl.transaction(new AssertFetch(dsl, "select count(*) as total from jooqtest_tx;", "1"));
    try {
        dsl.transaction(new ExecuteSql(dsl, "insert into jooqtest (name) values ('bar');", "insert into jooqtest (name) values ('foo');"));
        fail("A DataIntegrityViolationException should have been thrown.");
    } catch (DataIntegrityViolationException ex) {
    // Ignore
    }
    dsl.transaction(new AssertFetch(dsl, "select count(*) as total from jooqtest_tx;", "1"));
}
Also used : DSLContext(org.jooq.DSLContext) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 15 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project spring-framework by spring-projects.

the class SQLExceptionSubclassTranslatorTests method errorCodeTranslation.

@Test
public void errorCodeTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 0);
    DataIntegrityViolationException divex = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx);
    assertEquals(dataIntegrityViolationEx, divex.getCause());
    SQLException featureNotSupEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0);
    InvalidDataAccessApiUsageException idaex = (InvalidDataAccessApiUsageException) sext.translate("task", "SQL", featureNotSupEx);
    assertEquals(featureNotSupEx, idaex.getCause());
    SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0);
    DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2);
    assertEquals(dataIntegrityViolationEx2, divex2.getCause());
    SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0);
    PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx);
    assertEquals(permissionDeniedEx, pdaex.getCause());
    SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0);
    DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccessResourceEx);
    assertEquals(dataAccessResourceEx, darex.getCause());
    SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2);
    assertEquals("SQL2", bsgex2.getSql());
    assertEquals(badSqlEx2, bsgex2.getSQLException());
    SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0);
    ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx);
    assertEquals(tranRollbackEx, cfex.getCause());
    SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0);
    TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx);
    assertEquals(transientConnEx, tdarex.getCause());
    SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0);
    QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2);
    assertEquals(transientConnEx2, tdarex2.getCause());
    SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0);
    RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx);
    assertEquals(recoverableEx, rdaex2.getCause());
    // Test classic error code translation. We should move there next if the exception we pass in is not one
    // of the new sub-classes.
    SQLException sexEct = new SQLException("", "", 1);
    BadSqlGrammarException bsgEct = (BadSqlGrammarException) sext.translate("task", "SQL-ECT", sexEct);
    assertEquals("SQL-ECT", bsgEct.getSql());
    assertEquals(sexEct, bsgEct.getSQLException());
    // Test fallback. We assume that no database will ever return this error code,
    // but 07xxx will be bad grammar picked up by the fallback SQLState translator
    SQLException sexFbt = new SQLException("", "07xxx", 666666666);
    BadSqlGrammarException bsgFbt = (BadSqlGrammarException) sext.translate("task", "SQL-FBT", sexFbt);
    assertEquals("SQL-FBT", bsgFbt.getSql());
    assertEquals(sexFbt, bsgFbt.getSQLException());
    // and 08xxx will be data resource failure (non-transient) picked up by the fallback SQLState translator
    SQLException sexFbt2 = new SQLException("", "08xxx", 666666666);
    DataAccessResourceFailureException darfFbt = (DataAccessResourceFailureException) sext.translate("task", "SQL-FBT2", sexFbt2);
    assertEquals(sexFbt2, darfFbt.getCause());
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) QueryTimeoutException(org.springframework.dao.QueryTimeoutException) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) RecoverableDataAccessException(org.springframework.dao.RecoverableDataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Aggregations

DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)22 Test (org.junit.Test)9 SQLException (java.sql.SQLException)7 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)5 Set (java.util.Set)5 SimpleJdbcInsert (org.springframework.jdbc.core.simple.SimpleJdbcInsert)5 ResultSet (java.sql.ResultSet)4 DataAccessException (org.springframework.dao.DataAccessException)4 Date (java.util.Date)3 List (java.util.List)3 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)3 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)3 CabinetException (cz.metacentrum.perun.cabinet.bl.CabinetException)2 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 ConsistencyErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException)2 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)2 ArrayList (java.util.ArrayList)2 DSLContext (org.jooq.DSLContext)2 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)2