Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One connection in spring transaction?

I have few questions related to connections and spring transactions.

  1. Does spring use the same connection instance when multiple methods performing DML & DDL operations are executed in a transaction (propagation level REQUIRED)? I have read that it does maintain the same connection but do not know why and how does it do it technically? While explaining how if any hints in the spring source code are provided it would be helpful.

  2. Using Spring Declarative Transactions if I use Serializable as the isolation level, would spring make sure one connection is always used while performing database operations in that method or in any other method called from the original transactional method?

Are there any points I should keep in mind while working with Spring Transactions considering this topic?

Any thoughts/help on this topic would be appreciated. Thanks.

Update 1 - Sorry by mistake I had written serializable propagation level instead of isolation level. Corrected it.

like image 471
Andy Dufresne Avatar asked May 17 '11 14:05

Andy Dufresne


1 Answers

  1. Spring transaction management is just a unified interface to different transactional resources, such as JDBC connections. Since for most of transactional resources it doesn't make sense to have a transaction that spreads across multiple connections, all operations in Spring-managed transactions for these resources are executed in the same connection. Of course, if you use distributed transactions with JtaTransactionManager, each transactional resource involved into distrubuted transaction would have its own connection.

  2. Transaction isolation levels have nothing to do with Spring transaction management. Their meaning is defined in the database theory. Also, they are not related to transaction propagation.

Spring implements this behaviour by stroing connections (such as JDBC Connections) as a part of thread-local state using TransactionSynchronizationManager. See, for example, DataSourceUtils.

like image 149
axtavt Avatar answered Nov 16 '22 02:11

axtavt