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());
}
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;
}
}
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;
}
});
}
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);
}
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();
}
Aggregations