Tag Archives: Transactions

Spring Transaction Gotcha

Previously a follow developer asked me to have a look at a strange transactional problem he was having with Spring.  He had a class like below.


public class TransactionProblem {
    public void entryPointToClass()

    private void doTransactionalProcessing() {
        //do some db stuff

He was wondering why the doTransactionalProcessing was not being executed in a transaction.  The reason is depending on the spring configuration.  Spring can use CGLIB to subclass you service class and delegate all calls to your service class.  It works something like below

public class TransactionProblemProxy extends TransactionProblem {

    public void entryPointToClass() {
        //spring starts the transaction here if the method is annotated @Transactional (its not)
        //spring commits the transaction here if the method is annotated @Transactional (its not)</pre>


So as you can see from the above code when we call our service class it actually calls the TransactionProblemProxy.  The method is not annotated transactional so no transaction is created.  The call to the private method doTransactionalProcessing is not done through spring so the annotation is ignored.The solution to the above issue is either make the doTransactionalProcessing public and call it seperately or change the spring configuration to use AspectJ.