简体   繁体   English

JdbcTemplate 与 TransactionManager 究竟如何协同工作?

[英]How exactly JdbcTemplate with TransactionManager works together?

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.据我了解,DataSourceTransactionManager 将一个 JDBC 连接从指定的数据源绑定到当前线程,允许每个数据源有一个线程绑定的连接。 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.在此之后,如果我在事务中使用 JdbcTemplate,它将捕获由 DataSourceTransactionManager 绑定的连接。 Do I understand the mechanism correctly?我是否正确理解了该机制? A there any requirements for making transaction manager bean definition (ie singleton)? A 制作事务管理器 bean 定义(即单例)有什么要求吗?

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. Spring 的事务同步逻辑在保持事务在 JDBC、Hibernate、JPA 等之间保持同步方面承担着相当可怕的责任,最终结果非常无缝。

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.任何一笔交易都只由一个交易管理器管理,如果您有几个交易管理器,那么您将陷入痛苦的世界。

The default value is 15 seconds for lifetime of the connection acquired from the DataSource.从数据源获取的连接的生命周期的默认值为 15 秒。 And you can specify max connection timeout explicitly in your connection property file.您可以在连接属性文件中明确指定最大连接超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM