presto-main/src/main/java/io/prestosql/transaction/TransactionBuilder.java/TransactionBuilder/execute
class TransactionBuilder:    public <T> T execute(Session session, Function<Session, T> callback)
    {
        requireNonNull(session, "session is null");
        requireNonNull(callback, "callback is null");

        boolean managedTransaction = !session.getTransactionId().isPresent();

        Session transactionSession;
        if (managedTransaction) {
            TransactionId transactionId = transactionManager.beginTransaction(isolationLevel, readOnly, singleStatement);
            transactionSession = session.beginTransactionId(transactionId, transactionManager, accessControl);
        }
        else {
            // Check if we can merge with the existing transaction
            TransactionInfo transactionInfo = transactionManager.getTransactionInfo(session.getTransactionId().get());
            checkState(transactionInfo.getIsolationLevel().meetsRequirementOf(isolationLevel), "Cannot provide %s isolation with existing transaction isolation: %s", isolationLevel, transactionInfo.getIsolationLevel());
            checkState(!transactionInfo.isReadOnly() || readOnly, "Cannot provide read-write semantics with existing read-only transaction");
            checkState(!transactionInfo.isAutoCommitContext() && !singleStatement, "Cannot combine auto commit transactions");
            transactionSession = session;
        }

        boolean success = false;
        try {
            T result = callback.apply(transactionSession);
            success = true;
            return result;
        }
        finally {
            if (managedTransaction && transactionManager.transactionExists(transactionSession.getTransactionId().get())) {
                if (success) {
                    getFutureValue(transactionManager.asyncCommit(transactionSession.getTransactionId().get()));
                }
                else {
                    transactionManager.asyncAbort(transactionSession.getTransactionId().get());
                }
            }
        }
    }