Search in sources :

Example 6 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 7 with DataIntegrityViolationException

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

the class SQLErrorCodeSQLExceptionTranslator method doTranslate.

@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
    SQLException sqlEx = ex;
    if (sqlEx instanceof BatchUpdateException && sqlEx.getNextException() != null) {
        SQLException nestedSqlEx = sqlEx.getNextException();
        if (nestedSqlEx.getErrorCode() > 0 || nestedSqlEx.getSQLState() != null) {
            logger.debug("Using nested SQLException from the BatchUpdateException");
            sqlEx = nestedSqlEx;
        }
    }
    // First, try custom translation from overridden method.
    DataAccessException dex = customTranslate(task, sql, sqlEx);
    if (dex != null) {
        return dex;
    }
    // Next, try the custom SQLException translator, if available.
    if (this.sqlErrorCodes != null) {
        SQLExceptionTranslator customTranslator = this.sqlErrorCodes.getCustomSqlExceptionTranslator();
        if (customTranslator != null) {
            DataAccessException customDex = customTranslator.translate(task, sql, sqlEx);
            if (customDex != null) {
                return customDex;
            }
        }
    }
    // Check SQLErrorCodes with corresponding error code, if available.
    if (this.sqlErrorCodes != null) {
        String errorCode;
        if (this.sqlErrorCodes.isUseSqlStateForTranslation()) {
            errorCode = sqlEx.getSQLState();
        } else {
            // Try to find SQLException with actual error code, looping through the causes.
            // E.g. applicable to java.sql.DataTruncation as of JDK 1.6.
            SQLException current = sqlEx;
            while (current.getErrorCode() == 0 && current.getCause() instanceof SQLException) {
                current = (SQLException) current.getCause();
            }
            errorCode = Integer.toString(current.getErrorCode());
        }
        if (errorCode != null) {
            // Look for defined custom translations first.
            CustomSQLErrorCodesTranslation[] customTranslations = this.sqlErrorCodes.getCustomTranslations();
            if (customTranslations != null) {
                for (CustomSQLErrorCodesTranslation customTranslation : customTranslations) {
                    if (Arrays.binarySearch(customTranslation.getErrorCodes(), errorCode) >= 0) {
                        if (customTranslation.getExceptionClass() != null) {
                            DataAccessException customException = createCustomException(task, sql, sqlEx, customTranslation.getExceptionClass());
                            if (customException != null) {
                                logTranslation(task, sql, sqlEx, true);
                                return customException;
                            }
                        }
                    }
                }
            }
            // Next, look for grouped error codes.
            if (Arrays.binarySearch(this.sqlErrorCodes.getBadSqlGrammarCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new BadSqlGrammarException(task, sql, sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getInvalidResultSetAccessCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new InvalidResultSetAccessException(task, sql, sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDuplicateKeyCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DuplicateKeyException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDataIntegrityViolationCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DataIntegrityViolationException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getPermissionDeniedCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new PermissionDeniedDataAccessException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDataAccessResourceFailureCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DataAccessResourceFailureException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getTransientDataAccessResourceCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new TransientDataAccessResourceException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getCannotAcquireLockCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new CannotAcquireLockException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDeadlockLoserCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DeadlockLoserDataAccessException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getCannotSerializeTransactionCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new CannotSerializeTransactionException(buildMessage(task, sql, sqlEx), sqlEx);
            }
        }
    }
    // We couldn't identify it more precisely - let's hand it over to the SQLState fallback translator.
    if (logger.isDebugEnabled()) {
        String codes;
        if (this.sqlErrorCodes != null && this.sqlErrorCodes.isUseSqlStateForTranslation()) {
            codes = "SQL state '" + sqlEx.getSQLState() + "', error code '" + sqlEx.getErrorCode();
        } else {
            codes = "Error code '" + sqlEx.getErrorCode() + "'";
        }
        logger.debug("Unable to translate SQLException with " + codes + ", will now try the fallback translator");
    }
    return null;
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) CannotAcquireLockException(org.springframework.dao.CannotAcquireLockException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) InvalidResultSetAccessException(org.springframework.jdbc.InvalidResultSetAccessException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) CannotSerializeTransactionException(org.springframework.dao.CannotSerializeTransactionException) DeadlockLoserDataAccessException(org.springframework.dao.DeadlockLoserDataAccessException) DataAccessException(org.springframework.dao.DataAccessException) DeadlockLoserDataAccessException(org.springframework.dao.DeadlockLoserDataAccessException) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) BatchUpdateException(java.sql.BatchUpdateException)

Example 8 with DataIntegrityViolationException

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

the class SQLErrorCodeSQLExceptionTranslatorTests method errorCodeTranslation.

@Test
public void errorCodeTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException badSqlEx = new SQLException("", "", 1);
    BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", badSqlEx);
    assertEquals("SQL", bsgex.getSql());
    assertEquals(badSqlEx, bsgex.getSQLException());
    SQLException invResEx = new SQLException("", "", 4);
    InvalidResultSetAccessException irsex = (InvalidResultSetAccessException) sext.translate("task", "SQL", invResEx);
    assertEquals("SQL", irsex.getSql());
    assertEquals(invResEx, irsex.getSQLException());
    checkTranslation(sext, 5, DataAccessResourceFailureException.class);
    checkTranslation(sext, 6, DataIntegrityViolationException.class);
    checkTranslation(sext, 7, CannotAcquireLockException.class);
    checkTranslation(sext, 8, DeadlockLoserDataAccessException.class);
    checkTranslation(sext, 9, CannotSerializeTransactionException.class);
    checkTranslation(sext, 10, DuplicateKeyException.class);
    SQLException dupKeyEx = new SQLException("", "", 10);
    DataAccessException dksex = sext.translate("task", "SQL", dupKeyEx);
    assertTrue("Not instance of DataIntegrityViolationException", DataIntegrityViolationException.class.isAssignableFrom(dksex.getClass()));
    // 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 sex = new SQLException("", "07xxx", 666666666);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", sex);
    assertEquals("SQL2", bsgex2.getSql());
    assertEquals(sex, bsgex2.getSQLException());
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) SQLException(java.sql.SQLException) InvalidResultSetAccessException(org.springframework.jdbc.InvalidResultSetAccessException) DataAccessException(org.springframework.dao.DataAccessException) DeadlockLoserDataAccessException(org.springframework.dao.DeadlockLoserDataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 9 with DataIntegrityViolationException

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

the class SQLErrorCodeSQLExceptionTranslatorTests method customExceptionTranslation.

@Test
public void customExceptionTranslation() {
    final String TASK = "TASK";
    final String SQL = "SQL SELECT *";
    final SQLErrorCodes customErrorCodes = new SQLErrorCodes();
    final CustomSQLErrorCodesTranslation customTranslation = new CustomSQLErrorCodesTranslation();
    customErrorCodes.setBadSqlGrammarCodes(new String[] { "1", "2" });
    customErrorCodes.setDataIntegrityViolationCodes(new String[] { "3", "4" });
    customTranslation.setErrorCodes(new String[] { "1" });
    customTranslation.setExceptionClass(CustomErrorCodeException.class);
    customErrorCodes.setCustomTranslations(new CustomSQLErrorCodesTranslation[] { customTranslation });
    SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
    sext.setSqlErrorCodes(customErrorCodes);
    // Should custom translate this
    SQLException badSqlEx = new SQLException("", "", 1);
    assertEquals(CustomErrorCodeException.class, sext.translate(TASK, SQL, badSqlEx).getClass());
    assertEquals(badSqlEx, sext.translate(TASK, SQL, badSqlEx).getCause());
    // Shouldn't custom translate this
    SQLException invResEx = new SQLException("", "", 3);
    DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, invResEx);
    assertEquals(invResEx, diex.getCause());
    // Shouldn't custom translate this - invalid class
    exception.expect(IllegalArgumentException.class);
    customTranslation.setExceptionClass(String.class);
}
Also used : SQLException(java.sql.SQLException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 10 with DataIntegrityViolationException

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

the class SQLErrorCodeSQLExceptionTranslatorTests method customTranslateMethodTranslation.

@SuppressWarnings("serial")
@Test
public void customTranslateMethodTranslation() {
    final String TASK = "TASK";
    final String SQL = "SQL SELECT *";
    final DataAccessException customDex = new DataAccessException("") {
    };
    final SQLException badSqlEx = new SQLException("", "", 1);
    SQLException intVioEx = new SQLException("", "", 6);
    SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator() {

        @Override
        protected DataAccessException customTranslate(String task, String sql, SQLException sqlex) {
            assertEquals(TASK, task);
            assertEquals(SQL, sql);
            return (sqlex == badSqlEx) ? customDex : null;
        }
    };
    sext.setSqlErrorCodes(ERROR_CODES);
    // Shouldn't custom translate this
    assertEquals(customDex, sext.translate(TASK, SQL, badSqlEx));
    DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, intVioEx);
    assertEquals(intVioEx, diex.getCause());
}
Also used : SQLException(java.sql.SQLException) DataAccessException(org.springframework.dao.DataAccessException) DeadlockLoserDataAccessException(org.springframework.dao.DeadlockLoserDataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Aggregations

DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)27 Test (org.junit.Test)11 SQLException (java.sql.SQLException)8 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)5 Set (java.util.Set)5 DataAccessException (org.springframework.dao.DataAccessException)5 SimpleJdbcInsert (org.springframework.jdbc.core.simple.SimpleJdbcInsert)5 ResultSet (java.sql.ResultSet)4 Date (java.util.Date)3 List (java.util.List)3 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)3 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)3 MongoException (com.mongodb.MongoException)2 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 ObjectId (org.bson.types.ObjectId)2