Search in sources :

Example 6 with Transaction

use of org.jdbi.v3.sqlobject.transaction.Transaction 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

Handle (org.jdbi.v3.core.Handle)5 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 TransactionException (org.jdbi.v3.core.transaction.TransactionException)2 TransactionIsolationLevel (org.jdbi.v3.core.transaction.TransactionIsolationLevel)2 Transaction (org.jdbi.v3.sqlobject.transaction.Transaction)2 InvocationHandler (java.lang.reflect.InvocationHandler)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 BiConsumer (java.util.function.BiConsumer)1 User (jdbi.doc.ResultsTest.User)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1