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 Thre开发者_如何转开发ad 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. 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)?
"Multiple threads may concurrently be associated with the same global transaction." - JTA spec v1.1, section 3.2, page 13.
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.
精彩评论