Package org.springframework.dao

Examples of org.springframework.dao.DataAccessException


    }
    if (sql == null) {
      sql = "";
    }

    DataAccessException dex = doTranslate(task, sql, ex);
    if (dex != null) {
      // Specific exception match found.
      return dex;
    }
    // Looking for a fallback...
View Full Code Here


      EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
      tx.commit();
    }
    catch (RollbackException ex) {
      if (ex.getCause() instanceof RuntimeException) {
        DataAccessException dex = getJpaDialect().translateExceptionIfPossible((RuntimeException) ex.getCause());
        if (dex != null) {
          throw dex;
        }
      }
      throw new TransactionSystemException("Could not commit JPA transaction", ex);
View Full Code Here

      }
      // Don't close the EntityManager... That's up to the user.
    }

    private RuntimeException convertCompletionException(RuntimeException ex) {
      DataAccessException daex = (this.exceptionTranslator != null) ?
          this.exceptionTranslator.translateExceptionIfPossible(ex) :
          EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
      return (daex != null ? daex : ex);
    }
View Full Code Here

    if (sql == null) {
      sql = "";
    }
   
    // First, try custom translation from overridden method.
    DataAccessException dex = customTranslate(task, sql, sqlEx);
    if (dex != null) {
      return dex;
    }

    // Check SQLErrorCodes with corresponding error code, if available.
    if (this.sqlErrorCodes != null) {
      String errorCode = null;
      if (this.sqlErrorCodes.isUseSqlStateForTranslation()) {
        errorCode = sqlEx.getSQLState();
      }
      else {
        errorCode = Integer.toString(sqlEx.getErrorCode());
      }

      if (errorCode != null) {

        // Look for defined custom translations first.
        CustomSQLErrorCodesTranslation[] customTranslations = this.sqlErrorCodes.getCustomTranslations();
        if (customTranslations != null) {
          for (int i = 0; i < customTranslations.length; i++) {
            CustomSQLErrorCodesTranslation customTranslation = customTranslations[i];
            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;
                }
View Full Code Here

    assertEquals(sex, bsgex2.getSQLException());
  }

  private void checkTranslation(SQLExceptionTranslator sext, int errorCode, Class exClass) {
    SQLException sex = new SQLException("", "", errorCode);
    DataAccessException ex = sext.translate("", "", sex);
    assertTrue(exClass.isInstance(ex));
    assertTrue(ex.getCause() == sex);
  }
View Full Code Here


  public void testCustomTranslateMethodTranslation() {
    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() {
View Full Code Here


  private void doTest(String sqlState, Class dataAccessExceptionType) {
    SQLException ex = new SQLException(REASON, sqlState);
    SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator();
    DataAccessException dax = translator.translate(TASK, SQL, ex);
    assertNotNull("Translation must *never* result in a null DataAccessException being returned.", dax);
    assertEquals("Wrong DataAccessException type returned as the result of the translation", dataAccessExceptionType, dax.getClass());
    assertNotNull("The original SQLException must be preserved in the translated DataAccessException", dax.getCause());
    assertSame("The exact same original SQLException must be preserved in the translated DataAccessException", ex, dax.getCause());
  }
View Full Code Here

    return namedParameterJdbcTemplate;
  }

  protected DataAccessException translateException(String task_, String sql, Exception ex_) {
    if (ex_ instanceof SQLException) {
      DataAccessException cause = daoExceptionTranslator.translate(this.getClass().getName(), sql,
          (SQLException) ex_);
      throw ApplicationException.exception(cause, DAOMessageID.SQL_EXCEPTION.getMessage());
    } else if (ex_ instanceof DataAccessException) {
      Throwable cause = ex_.getCause();
      if (cause != null) {
        throw ApplicationException.exception(cause, DAOMessageID.SQL_EXCEPTION.getMessage(cause.getMessage()));
      } else {
        throw ApplicationException.exception(ex_, DAOMessageID.SQL_EXCEPTION.getMessage(ex_.getMessage()));
      }
    }
    throw ApplicationException.exception(
View Full Code Here

     * @param re the RuntimeException to translate to a Rave application exception
     * @return a Rave application exception representing the database error
     */
    @Override
    public DataAccessException translateExceptionIfPossible(RuntimeException re) {       
        DataAccessException e = null;
        // first make sure the root exception is actually an org.h2.jdbc.JdbcSQLException
        if (ExceptionUtils.getRootCause(re) instanceof JdbcSQLException) {
            JdbcSQLException rootException = (JdbcSQLException)ExceptionUtils.getRootCause(re);
                       
            // now translate the H2 specific error codes into Rave's common application Exceptions
View Full Code Here


  protected DataAccessException translateException(String task_, String sql_,
      Exception ex_) {
    if (ex_ instanceof SQLException) {
      DataAccessException cause = _daoExceptionTranslator.translate(this.getClass().getName(),
          sql_, (SQLException) ex_);
      throw ApplicationException.exception(cause,DAOMessageID.SQL_EXCEPTION.getMessage());
    } else if (ex_ instanceof DataAccessException){
      Throwable cause = ex_.getCause();
      throw ApplicationException.exception(cause, DAOMessageID.SQL_EXCEPTION.getMessage(cause.getMessage()));
    }
    throw ApplicationException.exception(String.format(
        "Failed to execute %s.%s with SQL=%s", this.getClass()
            .getName(), task_, sql_), ex_,DAOMessageID.DAO_FAILURE.getMessage());
  }
View Full Code Here

TOP

Related Classes of org.springframework.dao.DataAccessException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.