One connection in spring transaction?
Asked Answered
H

1

10

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.

Homerus answered 17/5, 2011 at 14:48 Comment(0)
C
8
  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.

Conterminous answered 17/5, 2011 at 15:0 Comment(7)
Can you please elaborate on the below points. 1. "Since for most of transactional resources it doesn't make sense to have a transaction that spreads across multiple connections". Which transactional resources are you referring to? 2. How does spring internally manage to get the same connection for all the db operations done in a transaction? (If you can point of some spring code I can have a look at it would be great).Homerus
In the question by mistake I added serializable propagation level instead of isolation level. Corrected it.Homerus
@Amit: Transactional resource are resources that provide transactional behaviour. Basicaly, it's databases and message queues.Conterminous
and How does spring internally manage to get the same connection for all the db operations done in a transaction? (If you can point of some spring code I can have a look at it would be great).Homerus
@axtavt: Thanks for spring source code update. From the TransactionSynchronizationManager implementation (bind and unbind resource methods) it looks like there would never be a scenario where multiple connection instances are used for a given data source when executing in a transaction right? Even in case of distributed transactions. Also I am still not clear on your point about why it does not make sense for have a transaction that spreads across multiple connections? What goes wrong if multiple connections are used in the same transaction?Homerus
@Amit: Yes. It's impossible to have a database transaction that spreads across multiple connections, because database transaction is inherently bound to the specifc connection.Conterminous
@Conterminous - Can you ellaborate on why a database transaction is inherently bound to a specific connection? Sorry if you are finding these follow up questions annoying but I am trying to clear my concepts around this.Homerus

© 2022 - 2024 — McMap. All rights reserved.