如何在多线程之间共享一个事务 [英] how to share one transaction between multi threads

查看:1117
本文介绍了如何在多线程之间共享一个事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们遇到了一个适用于多线程的场景。

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.

但是工作了几天,我发现一些帖子说JTA不支持多线程。
目前我们使用Bitronix作为JTA提供程序,有没有人知道Bitronix是否支持一个Transaction中的多线程?或者是否有任何其他JTA提供程序支持此(独立JTA提供程序而非J2EE容器)?

But work for several days, I found some posts say JTA does not support the multi Thread. currently we use Bitronix as the JTA provider, is there any body know if Bitronix support the multi thread in one Transaction? or is there any other JTA provider support this(standalone JTA provider not J2EE container)?

推荐答案

多个线程可能同时关联同样的全球交易。
- JTA规范v1.1,第3.2节,第13页。

"Multiple threads may concurrently be associated with the same global transaction." - JTA spec v1.1, section 3.2, page 13.

JBossTS将处理没问题。除了检查交易行为之外,困难实际上并不是事务管理器。您还需要正确处理与资源管理器(即数据库)的连接。如果您在线程之间共享一个连接,则不一定会以串行方式运行任何加速,因为除非驱动程序支持高效多路复用,否则它可能成为瓶颈。另一方面,如果您使用多个连接,则需要确保驱动程序能够合理地实现isSameRM以避免2PC,并且如果线程需要看到彼此对数据库的未提交更改,则还允许事务分支锁共享(紧密耦合)。所以除了一个好的事务管理器之外,你还需要一个好的连接管理器,例如JCA实现和良好的数据库驱动程序。祝你好运。

JBossTS will handle that no problem. 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 i.e. 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. So in addition to a good transaction manager you're going to need a good connection manager e.g. JCA implementation and a good database driver. Good luck finding those.

这篇关于如何在多线程之间共享一个事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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