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);
}
}
};
}
Aggregations