How exactly JdbcTemplate with TransactionManager works together?

泄露秘密 提交于 2020-01-03 11:03:56

问题


As far as I understood DataSourceTransactionManager binds a JDBC connection from the specified DataSource to the current thread, allowing for one thread-bound Connection per DataSource. If it's a pool of connections, it will take one of the available connections.

After this if I use JdbcTemplate inside a transaction, it will capture a connection binded by DataSourceTransactionManager. Do I understand the mechanism correctly? A there any requirements for making transaction manager bean definition (i.e. singleton)?


回答1:


You've pretty much described how it works. Spring's transaction synchronization logic has the rather fearsome responsibility for keeping transactions synchronized across JDBC, Hibernate, JPA, etc, and the end result is pretty seamless.

Transaction managers must be singletons, yes. Any one transaction is managed by only one transaction manager, and if you have several of them lying around, you're in for a world of pain.




回答2:


The default value is 15 seconds for lifetime of the connection acquired from the DataSource. And you can specify max connection timeout explicitly in your connection propery.



来源:https://stackoverflow.com/questions/2250754/how-exactly-jdbctemplate-with-transactionmanager-works-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!