Search in sources :

Example 1 with HandleCallback

use of org.jdbi.v3.core.HandleCallback in project jdbi by jdbi.

the class Jdbi method open.

/**
 * Obtain a Handle to the data source wrapped by this Jdbi instance.
 * You own this expensive resource and are required to close it or
 * risk leaks.  Using a {@code try-with-resources} block is recommended.
 *
 * @return an open Handle instance
 * @see #useHandle(HandleConsumer)
 * @see #withHandle(HandleCallback)
 */
public Handle open() {
    try {
        final long start = System.nanoTime();
        Connection conn = connectionFactory.openConnection();
        final long stop = System.nanoTime();
        for (JdbiPlugin p : plugins) {
            conn = p.customizeConnection(conn);
        }
        StatementBuilder cache = statementBuilderFactory.get().createStatementBuilder(conn);
        Handle h = new Handle(config.createCopy(), transactionhandler.get(), cache, conn);
        for (JdbiPlugin p : plugins) {
            h = p.customizeHandle(h);
        }
        LOG.trace("Jdbi [{}] obtain handle [{}] in {}ms", this, h, (stop - start) / 1000000L);
        return h;
    } catch (SQLException e) {
        throw new ConnectionException(e);
    }
}
Also used : SQLException(java.sql.SQLException) JdbiPlugin(org.jdbi.v3.core.spi.JdbiPlugin) DefaultStatementBuilder(org.jdbi.v3.core.statement.DefaultStatementBuilder) StatementBuilder(org.jdbi.v3.core.statement.StatementBuilder) Connection(java.sql.Connection)

Example 2 with HandleCallback

use of org.jdbi.v3.core.HandleCallback in project jdbi by jdbi.

the class TransactionDecorator method decorateHandler.

@Override
public Handler decorateHandler(Handler base, Class<?> sqlObjectType, Method method) {
    final Transaction txnAnnotation = method.getAnnotation(Transaction.class);
    final TransactionIsolationLevel isolation = txnAnnotation.value();
    final boolean readOnly = txnAnnotation.readOnly();
    return (target, args, handle) -> {
        Handle h = handle.getHandle();
        if (h.isInTransaction()) {
            // Already in transaction. The outermost @Transaction method determines the transaction isolation level.
            TransactionIsolationLevel currentLevel = h.getTransactionIsolationLevel();
            if (currentLevel != isolation && isolation != TransactionIsolationLevel.UNKNOWN) {
                throw new TransactionException("Tried to execute nested @Transaction(" + isolation + "), " + "but already running in a transaction with isolation level " + currentLevel + ".");
            }
            if (h.isReadOnly() && !readOnly) {
                throw new TransactionException("Tried to execute a nested @Transaction(readOnly=false) " + "inside a readOnly transaction");
            }
            return base.invoke(target, args, handle);
        }
        HandleCallback<Object, Exception> callback = th -> base.invoke(target, args, handle);
        final boolean flipReadOnly = readOnly != h.isReadOnly();
        if (flipReadOnly) {
            h.setReadOnly(readOnly);
        }
        try {
            if (isolation == TransactionIsolationLevel.UNKNOWN) {
                return h.inTransaction(callback);
            } else {
                return h.inTransaction(isolation, callback);
            }
        } finally {
            if (flipReadOnly) {
                h.setReadOnly(!readOnly);
            }
        }
    };
}
Also used : HandlerDecorator(org.jdbi.v3.sqlobject.HandlerDecorator) HandleCallback(org.jdbi.v3.core.HandleCallback) TransactionIsolationLevel(org.jdbi.v3.core.transaction.TransactionIsolationLevel) Transaction(org.jdbi.v3.sqlobject.transaction.Transaction) Handle(org.jdbi.v3.core.Handle) Method(java.lang.reflect.Method) TransactionException(org.jdbi.v3.core.transaction.TransactionException) Handler(org.jdbi.v3.sqlobject.Handler) TransactionException(org.jdbi.v3.core.transaction.TransactionException) Transaction(org.jdbi.v3.sqlobject.transaction.Transaction) HandleCallback(org.jdbi.v3.core.HandleCallback) TransactionIsolationLevel(org.jdbi.v3.core.transaction.TransactionIsolationLevel) Handle(org.jdbi.v3.core.Handle)

Aggregations

Method (java.lang.reflect.Method)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Handle (org.jdbi.v3.core.Handle)1 HandleCallback (org.jdbi.v3.core.HandleCallback)1 JdbiPlugin (org.jdbi.v3.core.spi.JdbiPlugin)1 DefaultStatementBuilder (org.jdbi.v3.core.statement.DefaultStatementBuilder)1 StatementBuilder (org.jdbi.v3.core.statement.StatementBuilder)1 TransactionException (org.jdbi.v3.core.transaction.TransactionException)1 TransactionIsolationLevel (org.jdbi.v3.core.transaction.TransactionIsolationLevel)1 Handler (org.jdbi.v3.sqlobject.Handler)1 HandlerDecorator (org.jdbi.v3.sqlobject.HandlerDecorator)1 Transaction (org.jdbi.v3.sqlobject.transaction.Transaction)1