Search in sources :

Example 61 with DataAccessException

use of org.springframework.dao.DataAccessException in project jOOQ by jOOQ.

the class TransactionTest method testjOOQTransactionsNested.

@Test
public void testjOOQTransactionsNested() {
    AtomicBoolean rollback1 = new AtomicBoolean(false);
    AtomicBoolean rollback2 = new AtomicBoolean(false);
    try {
        // If using Spring transactions, we don't need the c1 reference
        dsl.transaction(c1 -> {
            dsl.insertInto(BOOK).set(BOOK.ID, 5).set(BOOK.AUTHOR_ID, 1).set(BOOK.TITLE, "Book 5").execute();
            assertEquals(5, dsl.fetchCount(BOOK));
            try {
                dsl.transaction(c2 -> {
                    for (int i = 0; i < 2; i++) dsl.insertInto(BOOK).set(BOOK.ID, 6).set(BOOK.AUTHOR_ID, 1).set(BOOK.TITLE, "Book 6").execute();
                    Assert.fail();
                });
            } catch (DataAccessException e) {
                rollback1.set(true);
            }
            assertEquals(5, dsl.fetchCount(BOOK));
            throw new org.jooq.exception.DataAccessException("Rollback");
        });
    }// Upon the constraint violation, the transaction must already have been rolled back
     catch (org.jooq.exception.DataAccessException e) {
        assertEquals("Rollback", e.getMessage());
        rollback2.set(true);
    }
    assertEquals(4, dsl.fetchCount(BOOK));
    assertTrue(rollback2.get());
    assertTrue(rollback2.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 62 with DataAccessException

use of org.springframework.dao.DataAccessException in project otter by alibaba.

the class DdlUtils method getShardKeyByDRDS.

/**
     * 获取DRDS下表的拆分字段, 返回格式为 id,name
     * 
     * @param dataSource
     * @param schemaName
     * @param tableName
     * @return
     */
public static String getShardKeyByDRDS(final JdbcTemplate jdbcTemplate, final String catalogName, final String schemaName, final String tableName) {
    try {
        return (String) jdbcTemplate.execute("show partitions from ?", new PreparedStatementCallback() {

            public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                DatabaseMetaData metaData = ps.getConnection().getMetaData();
                // String sName = getIdentifierName(schemaName, metaData);
                String convertTableName = tableName;
                if (metaData.storesUpperCaseIdentifiers()) {
                    convertTableName = tableName.toUpperCase();
                }
                if (metaData.storesLowerCaseIdentifiers()) {
                    convertTableName = tableName.toLowerCase();
                }
                String tName = convertTableName;
                ps.setString(1, tName);
                ResultSet rs = ps.executeQuery();
                String log = null;
                if (rs.next()) {
                    log = rs.getString("KEYS");
                }
                rs.close();
                return log;
            }
        });
    } catch (DataAccessException e) {
        // 兼容下oracle源库和目标库DRDS表名不一致的情况,识别一下表名不存在
        Throwable cause = e.getRootCause();
        if (cause instanceof SQLException) {
            // ER_NO_SUCH_TABLE
            if (((SQLException) cause).getErrorCode() == 1146) {
                return null;
            }
        }
        throw e;
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatementCallback(org.springframework.jdbc.core.PreparedStatementCallback) PreparedStatement(java.sql.PreparedStatement) DatabaseMetaData(java.sql.DatabaseMetaData) DataAccessException(org.springframework.dao.DataAccessException)

Example 63 with DataAccessException

use of org.springframework.dao.DataAccessException in project vladmihalcea.wordpress.com by vladmihalcea.

the class DatabaseScriptLifecycleHandler method runInitScripts.

private void runInitScripts() {
    final ResourceDatabasePopulator resourceDatabasePopulator = createResourceDatabasePopulator();
    jdbcTemplate.execute(new ConnectionCallback<Void>() {

        @Override
        public Void doInConnection(Connection con) throws SQLException, DataAccessException {
            resourceDatabasePopulator.setScripts(getInitScripts());
            resourceDatabasePopulator.populate(con);
            return null;
        }
    });
}
Also used : SQLException(java.sql.SQLException) ResourceDatabasePopulator(org.springframework.jdbc.datasource.init.ResourceDatabasePopulator) Connection(java.sql.Connection) DataAccessException(org.springframework.dao.DataAccessException)

Example 64 with DataAccessException

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

the class DataAccessUtils method translateIfNecessary.

/**
	 * Return a translated exception if this is appropriate,
	 * otherwise return the input exception.
	 * @param rawException exception we may wish to translate
	 * @param pet PersistenceExceptionTranslator to use to perform the translation
	 * @return a translated exception if translation is possible, or
	 * the raw exception if it is not
	 */
public static RuntimeException translateIfNecessary(RuntimeException rawException, PersistenceExceptionTranslator pet) {
    Assert.notNull(pet, "PersistenceExceptionTranslator must not be null");
    DataAccessException dex = pet.translateExceptionIfPossible(rawException);
    return (dex != null ? dex : rawException);
}
Also used : DataAccessException(org.springframework.dao.DataAccessException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) TypeMismatchDataAccessException(org.springframework.dao.TypeMismatchDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 65 with DataAccessException

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

the class NamedParameterJdbcTemplateTests method testQueryWithResultSetExtractor.

@Test
public void testQueryWithResultSetExtractor() throws SQLException {
    given(resultSet.next()).willReturn(true);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
    params.put("country", "UK");
    Customer cust = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, new ResultSetExtractor<Customer>() {

        @Override
        public Customer extractData(ResultSet rs) throws SQLException, DataAccessException {
            rs.next();
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }
    });
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
    verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
    verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
    verify(preparedStatement).setString(2, "UK");
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Aggregations

DataAccessException (org.springframework.dao.DataAccessException)89 SQLException (java.sql.SQLException)40 Test (org.junit.Test)26 Connection (java.sql.Connection)17 ResultSet (java.sql.ResultSet)16 PreparedStatement (java.sql.PreparedStatement)14 MongoException (com.mongodb.MongoException)13 Document (org.bson.Document)8 TransactionStatus (org.springframework.transaction.TransactionStatus)7 HashMap (java.util.HashMap)6 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)5 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)5 IOException (java.io.IOException)4 ConnectionCallback (org.springframework.jdbc.core.ConnectionCallback)4 SpringSqlParams (com.opengamma.elsql.SpringSqlParams)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 Statement (java.sql.Statement)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IJoinQueryString (org.apereo.portal.jdbc.IJoinQueryString)3