JdbcTemplate与TransactionManager到底如何协同工作? [英] How exactly JdbcTemplate with TransactionManager works together?

查看:239
本文介绍了JdbcTemplate与TransactionManager到底如何协同工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,DataSourceTransactionManager将JDBC连接从指定的DataSource绑定到当前线程,从而为每个DataSource允许一个线程绑定的Connection.如果它是一个连接池,它将使用一个可用的连接.

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.

此后,如果我在事务内使用JdbcTemplate,它将捕获由DataSourceTransactionManager绑定的连接.我是否正确理解该机制?制定交易管理器bean定义(即单例)有什么要求吗?

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)?

推荐答案

您已经介绍了它的工作原理. Spring的事务同步逻辑承担着使事务在JDBC,Hibernate,JPA等之间保持同步的可怕职责,最终结果是无缝的.

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.

这篇关于JdbcTemplate与TransactionManager到底如何协同工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆