Hello! I am migrating a project from Spring 3.x to Spring 4.3.25. The project I am working with has an expectation that the transaction cleanup phase happens after the transaction commit phase. It used to be like this when the transactional aspect was implemented using the before/after/after returning/after throwing pointcuts (AspectJ). However, it has changed and now it is impossible to execute before commit phase code that requires database connection (which is inside the transaction context).

As a workaround I have cloned the spring-aspects module and manually rewritten the transactional aspect, however that solution is not perfect.

Question: Why does the TransactionAspectSupport#invokeWithinTransaction method clean up the context before commit phase?

Can I create a pull request and move the commit phase inside the try section?

if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
    // Standard transaction demarcation with getTransaction and commit/rollback calls.
    TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
    Object retVal = null;
    try {
        // This is an around advice: Invoke the next interceptor in the chain.
        // This will normally result in a target object being invoked.
        retVal = invocation.proceedWithInvocation();
        // To here <---------------
        commitTransactionAfterReturning(txInfo);
    }
    catch (Throwable ex) {
        // target invocation exception
        completeTransactionAfterThrowing(txInfo, ex);
        throw ex;
    }
    finally {
        cleanupTransactionInfo(txInfo);
    }
    // From here <-----------
    // commitTransactionAfterReturning(txInfo);
    return retVal;
}

Comment From: snicoll

Unfortunately, we can't provide you support based on the description as I can't replicate the behavior that you've described based on this issue. Sorry this got overlooked but, given the issue is no longer actionable, I am going to close it.