use of org.seasar.doma.jdbc.JdbcException in project doma-spring-boot by domaframework.
the class DomaAutoConfigurationTest method testSQLExceptionTranslator.
@Test
public void testSQLExceptionTranslator() {
this.context.register(DomaAutoConfiguration.class, DataSourceAutoConfiguration.class);
this.context.refresh();
PersistenceExceptionTranslator translator = this.context.getBean(PersistenceExceptionTranslator.class);
{
// Translated by SQLErrorCodeSQLExceptionTranslator
DataAccessException dataAccessException = translator.translateExceptionIfPossible(new JdbcException(Message.DOMA2008, new SQLException("Acquire Lock on H2", "SqlState", 50200, null)));
assertThat(dataAccessException, is(instanceOf(CannotAcquireLockException.class)));
}
{
// Translated by SQLExceptionSubclassTranslator(fallback)
DataAccessException dataAccessException = translator.translateExceptionIfPossible(new JdbcException(Message.DOMA2008, new SQLTimeoutException("Timeout", "SqlState", -1, null)));
assertThat(dataAccessException, is(instanceOf(QueryTimeoutException.class)));
}
{
// Translated by SQLStateSQLExceptionTranslator (fallback)
DataAccessException dataAccessException = translator.translateExceptionIfPossible(new JdbcException(Message.DOMA2008, new SQLException("With check violation", "44", -1, null)));
assertThat(dataAccessException, is(instanceOf(DataIntegrityViolationException.class)));
}
}
use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.
the class ReservedIdProvider method getIdentities.
protected long[] getIdentities() {
long[] identities = new long[reservationSize];
Sql<?> sql = createSql();
JdbcLogger logger = config.getJdbcLogger();
Connection connection = JdbcUtil.getConnection(config.getDataSource());
try {
PreparedStatement preparedStatement = JdbcUtil.prepareStatement(connection, sql);
try {
logger.logSql(getClass().getName(), "getIdentities", sql);
setupOptions(preparedStatement);
ResultSet resultSet = preparedStatement.executeQuery();
try {
for (int i = 0; i < reservationSize && resultSet.next(); i++) {
identities[i] = resultSet.getLong(1);
}
} catch (final SQLException e) {
throw new JdbcException(Message.DOMA2083, e, entityType.getName(), e);
} finally {
JdbcUtil.close(resultSet, logger);
}
} catch (SQLException e) {
throw new JdbcException(Message.DOMA2083, e, entityType.getName(), e);
} finally {
JdbcUtil.close(preparedStatement, logger);
}
} finally {
JdbcUtil.close(connection, logger);
}
return identities;
}
use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.
the class AbstractSelectQuery method expandColumns.
protected List<String> expandColumns(ExpandNode node) {
if (entityType == null) {
SqlLocation location = node.getLocation();
throw new JdbcException(Message.DOMA2144, location.getSql(), location.getLineNumber(), location.getPosition());
}
Naming naming = config.getNaming();
Dialect dialect = config.getDialect();
return entityType.getEntityPropertyTypes().stream().map(p -> p.getColumnName(naming::apply, dialect::applyQuote)).collect(Collectors.toList());
}
use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.
the class LocalTransaction method beginInternal.
protected void beginInternal(TransactionIsolationLevel transactionIsolationLevel, @SuppressWarnings("SameParameterValue") String callerMethodName) {
assertNotNull(callerMethodName);
LocalTransactionContext context = localTxContextHolder.get();
if (isActiveInternal(context)) {
String id = context.getId();
rollbackInternal(callerMethodName);
throw new TransactionAlreadyBegunException(id);
}
context = getLocalTransactionContext();
context.begin(() -> {
Connection connection = JdbcUtil.getConnection(dataSource);
int transactionIsolation;
try {
transactionIsolation = connection.getTransactionIsolation();
} catch (SQLException e) {
closeConnection(connection);
throw new JdbcException(Message.DOMA2056, e, e);
}
if (transactionIsolationLevel != null && transactionIsolationLevel != TransactionIsolationLevel.DEFAULT) {
int level = transactionIsolationLevel.getLevel();
try {
connection.setTransactionIsolation(level);
} catch (SQLException e) {
closeConnection(connection);
throw new JdbcException(Message.DOMA2055, e, transactionIsolationLevel.name(), e);
}
}
boolean isAutoCommit;
try {
isAutoCommit = connection.getAutoCommit();
} catch (SQLException e) {
closeConnection(connection);
throw new JdbcException(Message.DOMA2084, e, e);
}
if (isAutoCommit) {
try {
connection.setAutoCommit(false);
} catch (SQLException e) {
closeConnection(connection);
throw new JdbcException(Message.DOMA2041, e, e);
}
}
return new LocalTransactionConnection(connection, transactionIsolation, isAutoCommit);
});
jdbcLogger.logTransactionBegun(className, callerMethodName, context.getId());
}
use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.
the class LocalTransaction method rollback.
/**
* Undoes all changes made after the given save point.
*
* @param savepointName the name of the save point
* @throws DomaNullPointerException if the {@code savepointName} is {@code null}
* @throws SavepointNotFoundException if the save point is not found
* @throws TransactionNotYetBegunException if this transaction is not yet begun
* @throws JdbcException if a JDBC related error occurs
*/
public void rollback(String savepointName) {
if (savepointName == null) {
rollbackInternal("rollback");
throw new DomaNullPointerException("savepointName");
}
LocalTransactionContext context = localTxContextHolder.get();
if (!isActiveInternal(context)) {
throw new TransactionNotYetBegunException(Message.DOMA2062, savepointName);
}
String id = context.getId();
Savepoint savepoint = context.getSavepoint(savepointName);
if (savepoint == null) {
rollbackInternal("rollback");
throw new SavepointNotFoundException(savepointName);
}
LocalTransactionConnection connection = context.getConnection();
try {
connection.rollback(savepoint);
} catch (SQLException e) {
rollbackInternal("rollback");
throw new JdbcException(Message.DOMA2052, e, savepointName, e);
}
jdbcLogger.logTransactionSavepointRolledback(className, "rollback", id, savepointName);
}
Aggregations