Spring @Transactional and JCBC autoCommit

In my actual application, I have a DBCP connection pool that does not have JCBC autoCommit = false set. It seems to have autoCommit = true. This is probably a mistake, but I would like to understand the effect of changing this parameter.

I use: - Spring with @Transactional annotation - Spring Package with JDBC readers and writers, ultimately custom mascots using JdbcTemplate

I would like to know if Spring really sets autoCommit = false for the current connection if it is in the context of the transaction handled by TransactionManager. Does it override the default value? Because it seems to me that it makes sense to do it.

+6
source share
1 answer

PlatformTransactionManager is an interface, so I would not say that all implementations set AutoCommit = false, however the most common implementation (DataSourceTransactionManager) sets AutoCommit = false. see the 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 sense to do this or you won’t have a rollback segment to activate rollbacks.

+8
source

Source: https://habr.com/ru/post/943971/


All Articles