简体   繁体   English

Java Spring事务管理

[英]Java Spring transaction management

Please excuse if this or similar question has been asked before. 如果之前曾询问此问题或类似问题,请原谅。 I'm a novice to Spring. 我是Spring的新手。 My question is how comes that transaction manager and JdbcTemplate are using the same DB connection. 我的问题是,事务管理器和JdbcTemplate是如何使用相同的数据库连接的。 In all the documenation and books I could lay my hands on it' said that both beans (transaction manager and JDBC template) are given a data source. 在所有的文档和书籍中,我可以把手放在它上面“说两个bean(事务管理器和JDBC模板)都有一个数据源。 Let it be Apache Commons BasicDataSource. 让它成为Apache Commons BasicDataSource。 As far as I understand, both objects when instantiated will call method getConnection() of the data source and will be given different connections. 据我所知,实例化时的两个对象都将调用数据源的方法getConnection()并给出不同的连接。 After that JDBC template's update will be performed on one connection while transaction manager's commit on another. 之后,JDBC模板的更新将在一个连接上执行,而事务管理器在另一个连接上提交。 Definitely no transactional behavior will occur. 绝对不会发生任何交易行为。

Where am I wrong? 我哪里错了?

Actually my program is a bit more complicated. 实际上我的程序有点复杂。 It is multithreaded and it manages a connection pool. 它是多线程的,它管理连接池。 Initially each thread gets "connection" (actually JdbcTemplate object) and then uses it until termination. 最初每个线程获得“连接”(实际上是JdbcTemplate对象),然后使用它直到终止。 During its life it performs a series of database update "batches" which must be transactional. 在其生命周期中,它执行一系列数据库更新“批处理”,这必须是事务性的。 But attempt to use @Transactional annotation fails - if there are two insert operations in a batch and the second one fails, then the first one is not rolled back. 但尝试使用@Transactional注释失败 - 如果批处理中有两个插入操作而第二个操作失败,则第一个不会回滚。

Both DataSourceTransactionManager and JdbcTemplate refer to the same BasicDataSource in beans.xml. DataSourceTransactionManager和JdbcTemplate都引用beans.xml中的相同BasicDataSource。

It's obvious that somewhere I'm terribly wrong in my understanding oа transaction management mecahism in Spring because other people use it happily. 很明显,在我对Spring中的事务管理mecahism的理解中,我非常错误,因为其他人愉快地使用它。 But where indeed? 但确实在哪里?

Actually you can look at the sources of JdbcTemplate . 实际上你可以看一下JdbcTemplate的来源。

You can see there that it doesn't get connections directly from datasource but does Connection con = DataSourceUtils.getConnection(getDataSource()); 你可以看到它没有直接从数据源获得连接,但Connection con = DataSourceUtils.getConnection(getDataSource()); instead. 代替。

These DataSourceUtils are responsible for getting current transaction connection from Spring thread-local connection holder or instantiation of new Connection from DataSource and registering it in the holder if it was first request in transaction for example. 这些DataSourceUtils负责从Spring线程本地连接持有者获取当前事务连接或从DataSource实例化新Connection,并在事务中首次请求时将其注册到持有者中。

PS And you can't get single transaction running across multiple threads with Spring transaction mechanism in any easy way. PS并且您无法以任何简单的方式使用Spring事务机制在多个线程上运行单个事务。

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

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