javaspringtransactionsspring-jdbc

How to log transaction begin, commit and rollbacks of Spring JDBC DataSourceTransactionManager?


I need to check the transaction commands of Spring JDBC.

How can I log the transaction begin, rollback and commit automatically called from Spring JDBC DataSourceTransactionManager with log4j?


Solution

  • You have to set the following class to DEBUG log level:

    org.springframework.jdbc.support.JdbcTransactionManager: DEBUG
    

    You will then see logs like this:

    ... o.s.jdbc.support.JdbcTransactionManager  : Participating in existing transaction
    ...
    ... o.s.jdbc.support.JdbcTransactionManager  : Committing JDBC transaction on Connection 
    

    If logging happens or not is decided inside the AbstractPlatformTransactionManager:

    boolean debugEnabled = logger.isDebugEnabled();
    ...
    return prepareTransactionStatus(def, null, true, newSynchronization, debugEnabled, null);
    

    As this class is the abstract superclass of the different available TransactionManagers, it refers at runtime to the logger of the specific sublass (like here JdbcTransactionManager). But there are also others like JtaTransactionManager, etc. Hence, you will have to adapt the logging config appropriately, if you use a different transaction manager.