简体   繁体   English

如何在多线程之间共享一个事务

[英]how to share one transaction between multi threads

We meet an scenario that works with multi thread. 我们遇到了一个适用于多线程的场景。

In the main Thread, do some logic and update the database, in a point, it will call another service to update database, which is run in another Thread. 在主线程中,做一些逻辑并更新数据库,在某一点上,它会调用另一个服务来更新数据库,这是在另一个线程中运行的。

We want the two Threads share the same transaction, that means, either operation in either Thread fails, then the operation in another Thread will also be rolled back. 我们希望两个线程共享相同的事务,这意味着,任一线程中的任一操作都失败,那么另一个线程中的操作也将被回滚。

But work for several days, I found some posts say JTA does not support the multi Thread. 但是工作了几天,我发现一些帖子说JTA不支持多线程。 currently we use Bitronix as the JTA provider, is there any body know if Bitronix support the multi thread in one Transaction? 目前我们使用Bitronix作为JTA提供程序,有没有人知道Bitronix是否支持一个Transaction中的多线程? or is there any other JTA provider support this(standalone JTA provider not J2EE container)? 或者是否有任何其他JTA提供程序支持此(独立JTA提供程序而非J2EE容器)?

"Multiple threads may concurrently be associated with the same global transaction." “多个线程可能同时与同一个全局事务相关联。” - JTA spec v1.1, section 3.2, page 13. - JTA规范v1.1,第3.2节,第13页。

JBossTS will handle that no problem. JBossTS将处理没问题。 Checked transaction behaviour aside, the difficulty is not really the transaction manager though. 除了检查交易行为之外,困难实际上并不是事务管理器。 You also need correct handling of the connection(s) to the resource manager ie database. 您还需要正确处理与资源管理器(即数据库)的连接。 If you share one connection between the threads you don't necessarily get any speedup over running serially, as it's a potential bottleneck unless the driver supports efficient multiplexing. 如果您在线程之间共享一个连接,则不一定会以串行方式运行任何加速,因为除非驱动程序支持高效多路复用,否则它可能是瓶颈。 On the other hand if you use multiple connections you need to ensure the driver is going to implement isSameRM sensibly to avoid 2PC and also allow transaction branch lock sharing (close coupling) if the Threads need to see one another's uncommitted changes to the db. 另一方面,如果您使用多个连接,则需要确保驱动程序能够合理地实现isSameRM以避免2PC,并且如果线程需要查看彼此对数据库的未提交更改,则还允许事务分支锁共享(紧密耦合)。 So in addition to a good transaction manager you're going to need a good connection manager eg JCA implementation and a good database driver. 所以除了一个好的事务管理器之外,你还需要一个好的连接管理器,例如JCA实现和一个好的数据库驱动程序。 Good luck finding those. 祝你好运。

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

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