19

On my actual application, I have a DBCP connection pool which doesn't have JDBC autoCommit=false set. It seems to have the default autoCommit=true. This is probably a mistake but I'd like to understand the impact of changing this parameter.

I am using: - Spring with @Transactional annotation - Spring Batch with JDBC readers and writers, eventually custom tasklets using JdbcTemplate

I would like to know if Spring does set autoCommit=false on the current connection if it is in the context of a transaction handled by the TransactionManager. Does it override the default setting? Because it seems to me it makes sense to do so.

Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
  • 3
    Yes, it does. Spring manages it for you with the annotation implementation class. – duffymo Apr 30 '13 at 13:38
  • thanks but you could have answered instead of a comment :) – Sebastien Lorber Apr 30 '13 at 14:22
  • 7
    Don't care. Everybody around here is an expert. When I give an answer, I have people telling me it should be a comment. I comment, and you tell me it should be an answer. Who cares? Sometimes I have time to answer, sometimes I don't. – duffymo Apr 30 '13 at 16:35

1 Answers1

16

PlatformTransactionManager is an interface, so I would not blanket say that all implementations set AutoCommit = false, however the most common implementation (DataSourceTransactionManager) does set AutoCommit = false. see code snippet below from the doBegin method:

if (con.getAutoCommit()) {
            txObject.setMustRestoreAutoCommit(true);
            if (logger.isDebugEnabled()) {
                logger.debug("Switching JDBC Connection [" + con + "] to manual commit");
            }
            con.setAutoCommit(false);
        }
        txObject.getConnectionHolder().setTransactionActive(true);

Now as you stated, it makes perfect sense to do so or you would not have a rollback segment to activate a rollback on.

fpmoles
  • 1,209
  • 8
  • 14