Search in sources :

Example 1 with DuplicateKeyException

use of org.springframework.dao.DuplicateKeyException 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 2 with DuplicateKeyException

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

the class MembersManagerImpl method createMember.

public Member createMember(PerunSession sess, Vo vo, User user) throws InternalErrorException, AlreadyMemberException {
    Member member = null;
    try {
        // Set the new Member id
        int newId = Utils.getNewId(jdbc, "members_id_seq");
        jdbc.update("insert into members (id, vo_id, user_id, status, created_by,created_at,modified_by,modified_at,created_by_uid,modified_by_uid) " + "values (?,?,?,?,?," + Compatibility.getSysdate() + ",?," + Compatibility.getSysdate() + ",?,?)", newId, vo.getId(), user.getId(), Status.INVALID.getCode(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), sess.getPerunPrincipal().getUserId());
        member = new Member(newId, user.getId(), vo.getId(), Status.INVALID);
    } catch (DuplicateKeyException e) {
        throw new AlreadyMemberException(e);
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
    return member;
}
Also used : Member(cz.metacentrum.perun.core.api.Member) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Example 3 with DuplicateKeyException

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

the class MailManagerImpl method copyMailsFromGroupToGroup.

@Override
public void copyMailsFromGroupToGroup(PerunSession sess, Group fromGroup, Group toGroup) throws PerunException {
    Vo fromVO = perun.getVosManagerBl().getVoById(registrarSession, fromGroup.getVoId());
    if (!AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, fromGroup) && !AuthzResolver.isAuthorized(sess, Role.VOADMIN, fromGroup) && !AuthzResolver.isAuthorized(sess, Role.TOPGROUPCREATOR, fromVO)) {
        throw new PrivilegeException(sess, "copyMailsFromGroupToGroup");
    }
    if (!AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, toGroup) && !AuthzResolver.isAuthorized(sess, Role.VOADMIN, toGroup)) {
        throw new PrivilegeException(sess, "copyMailsFromGroupToGroup");
    }
    ApplicationForm formFrom = registrarManager.getFormForGroup(fromGroup);
    ApplicationForm formTo = registrarManager.getFormForGroup(toGroup);
    List<ApplicationMail> mails = getApplicationMails(sess, formFrom);
    for (ApplicationMail mail : mails) {
        // to start transaction
        try {
            registrarManager.getMailManager().addMail(sess, formTo, mail);
        } catch (DuplicateKeyException ex) {
            log.info("[MAIL MANAGER] Mail notification of type {} skipped while copying (was already present).", mail.getMailType() + "/" + mail.getAppType());
        }
    }
}
Also used : ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) ApplicationMail(cz.metacentrum.perun.registrar.model.ApplicationMail)

Example 4 with DuplicateKeyException

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

the class MailManagerImpl method copyMailsFromVoToVo.

@Override
public void copyMailsFromVoToVo(PerunSession sess, Vo fromVo, Vo toVo) throws PerunException {
    if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, fromVo) || !AuthzResolver.isAuthorized(sess, Role.VOADMIN, toVo)) {
        throw new PrivilegeException(sess, "copyMailsFromVoToVo");
    }
    ApplicationForm formFrom = registrarManager.getFormForVo(fromVo);
    ApplicationForm formTo = registrarManager.getFormForVo(toVo);
    List<ApplicationMail> mails = getApplicationMails(sess, formFrom);
    for (ApplicationMail mail : mails) {
        // to start transaction
        try {
            registrarManager.getMailManager().addMail(sess, formTo, mail);
        } catch (DuplicateKeyException ex) {
            log.info("[MAIL MANAGER] Mail notification of type {} skipped while copying (was already present).", mail.getMailType() + "/" + mail.getAppType());
        }
    }
}
Also used : ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) ApplicationMail(cz.metacentrum.perun.registrar.model.ApplicationMail)

Example 5 with DuplicateKeyException

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

the class MailManagerImpl method copyMailsFromVoToGroup.

@Override
public void copyMailsFromVoToGroup(PerunSession sess, Vo fromVo, Group toGroup, boolean reverse) throws PerunException {
    if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, fromVo)) {
        throw new PrivilegeException(sess, "copyMailsFromVoToVo");
    }
    if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, toGroup) && !AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, toGroup)) {
        throw new PrivilegeException(sess, "copyMailsFromVoToVo");
    }
    if (reverse) {
        // copy notifications from Group to VO
        ApplicationForm voForm = registrarManager.getFormForVo(fromVo);
        ApplicationForm groupForm = registrarManager.getFormForGroup(toGroup);
        List<ApplicationMail> mails = getApplicationMails(sess, groupForm);
        for (ApplicationMail mail : mails) {
            // to start transaction
            try {
                registrarManager.getMailManager().addMail(sess, voForm, mail);
            } catch (DuplicateKeyException ex) {
                log.info("[MAIL MANAGER] Mail notification of type {} skipped while copying (was already present).", mail.getMailType() + "/" + mail.getAppType());
            }
        }
    } else {
        // copy notifications from VO to Group
        ApplicationForm voForm = registrarManager.getFormForVo(fromVo);
        ApplicationForm groupForm = registrarManager.getFormForGroup(toGroup);
        List<ApplicationMail> mails = getApplicationMails(sess, voForm);
        for (ApplicationMail mail : mails) {
            // to start transaction
            try {
                registrarManager.getMailManager().addMail(sess, groupForm, mail);
            } catch (DuplicateKeyException ex) {
                log.info("[MAIL MANAGER] Mail notification of type {} skipped while copying (was already present).", mail.getMailType() + "/" + mail.getAppType());
            }
        }
    }
}
Also used : ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) ApplicationMail(cz.metacentrum.perun.registrar.model.ApplicationMail)

Aggregations

DuplicateKeyException (org.springframework.dao.DuplicateKeyException)6 ApplicationForm (cz.metacentrum.perun.registrar.model.ApplicationForm)3 ApplicationMail (cz.metacentrum.perun.registrar.model.ApplicationMail)3 Member (cz.metacentrum.perun.core.api.Member)1 AlreadyMemberException (cz.metacentrum.perun.core.api.exceptions.AlreadyMemberException)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 BatchUpdateException (java.sql.BatchUpdateException)1 SQLException (java.sql.SQLException)1 CannotAcquireLockException (org.springframework.dao.CannotAcquireLockException)1 CannotSerializeTransactionException (org.springframework.dao.CannotSerializeTransactionException)1 DataAccessException (org.springframework.dao.DataAccessException)1 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)1 PermissionDeniedDataAccessException (org.springframework.dao.PermissionDeniedDataAccessException)1 TransientDataAccessResourceException (org.springframework.dao.TransientDataAccessResourceException)1 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)1 InvalidResultSetAccessException (org.springframework.jdbc.InvalidResultSetAccessException)1