Search in sources :

Example 36 with JdbcException

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)));
    }
}
Also used : QueryTimeoutException(org.springframework.dao.QueryTimeoutException) CannotAcquireLockException(org.springframework.dao.CannotAcquireLockException) DomaPersistenceExceptionTranslator(org.seasar.doma.boot.DomaPersistenceExceptionTranslator) PersistenceExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator) SQLException(java.sql.SQLException) SQLTimeoutException(java.sql.SQLTimeoutException) JdbcException(org.seasar.doma.jdbc.JdbcException) DataAccessException(org.springframework.dao.DataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 37 with JdbcException

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;
}
Also used : JdbcLogger(org.seasar.doma.jdbc.JdbcLogger) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JdbcException(org.seasar.doma.jdbc.JdbcException)

Example 38 with JdbcException

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());
}
Also used : SelectOptions(org.seasar.doma.jdbc.SelectOptions) BiFunction(java.util.function.BiFunction) EntityType(org.seasar.doma.jdbc.entity.EntityType) HashMap(java.util.HashMap) SelectOptionsAccessor(org.seasar.doma.jdbc.SelectOptionsAccessor) Function(java.util.function.Function) Dialect(org.seasar.doma.jdbc.dialect.Dialect) Naming(org.seasar.doma.jdbc.Naming) ExpandNode(org.seasar.doma.internal.jdbc.sql.node.ExpandNode) FetchType(org.seasar.doma.FetchType) SqlLocation(org.seasar.doma.internal.jdbc.sql.node.SqlLocation) Map(java.util.Map) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) JdbcException(org.seasar.doma.jdbc.JdbcException) BasicSingleResultHandler(org.seasar.doma.internal.jdbc.command.BasicSingleResultHandler) SqlNode(org.seasar.doma.jdbc.SqlNode) PreparedSql(org.seasar.doma.jdbc.PreparedSql) Value(org.seasar.doma.internal.expr.Value) SelectCommand(org.seasar.doma.jdbc.command.SelectCommand) Collectors(java.util.stream.Collectors) AssertionUtil.assertNotNull(org.seasar.doma.internal.util.AssertionUtil.assertNotNull) Message(org.seasar.doma.message.Message) List(java.util.List) SqlLogType(org.seasar.doma.jdbc.SqlLogType) PrimitiveLongWrapper(org.seasar.doma.wrapper.PrimitiveLongWrapper) Dialect(org.seasar.doma.jdbc.dialect.Dialect) SqlLocation(org.seasar.doma.internal.jdbc.sql.node.SqlLocation) Naming(org.seasar.doma.jdbc.Naming) JdbcException(org.seasar.doma.jdbc.JdbcException)

Example 39 with JdbcException

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());
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcException(org.seasar.doma.jdbc.JdbcException) Savepoint(java.sql.Savepoint)

Example 40 with JdbcException

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);
}
Also used : SQLException(java.sql.SQLException) DomaNullPointerException(org.seasar.doma.DomaNullPointerException) Savepoint(java.sql.Savepoint) JdbcException(org.seasar.doma.jdbc.JdbcException)

Aggregations

JdbcException (org.seasar.doma.jdbc.JdbcException)69 Test (org.junit.jupiter.api.Test)35 SqlNode (org.seasar.doma.jdbc.SqlNode)18 SQLException (java.sql.SQLException)15 BigDecimal (java.math.BigDecimal)9 Value (org.seasar.doma.internal.expr.Value)9 ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)8 SqlLocation (org.seasar.doma.internal.jdbc.sql.node.SqlLocation)7 SelectOptions (org.seasar.doma.jdbc.SelectOptions)7 SqlParser (org.seasar.doma.internal.jdbc.sql.SqlParser)6 Connection (java.sql.Connection)5 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)5 PreparedStatement (java.sql.PreparedStatement)4 Savepoint (java.sql.Savepoint)4 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 DomaNullPointerException (org.seasar.doma.DomaNullPointerException)4 MockConnection (org.seasar.doma.internal.jdbc.mock.MockConnection)4 MockDataSource (org.seasar.doma.internal.jdbc.mock.MockDataSource)4 IfBlockNode (org.seasar.doma.internal.jdbc.sql.node.IfBlockNode)4