Search in sources :

Example 1 with TransactionIsolationLevel

use of org.jdbi.v3.core.transaction.TransactionIsolationLevel 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 Handle (org.jdbi.v3.core.Handle)1 HandleCallback (org.jdbi.v3.core.HandleCallback)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