简体   繁体   English

是否使用JTA交易?

[英]use JTA transaction or not?

I am developing a J2EE application which is deployed on JBoss application server. 我正在开发一个部署在JBoss应用服务器上的J2EE应用程序。 The application is composed of an EJB 2.x component and a web component and is running on local computer or remote server. 该应用程序由EJB 2.x组件和Web组件组成,并在本地计算机或远程服务器上运行。 The database is either Oracle or SQL Server and is not in a distributed envrionment. 数据库是Oracle或SQL Server,不在分布式环境中。

I am using Hibernate 3.6 (JPA 2.0 implementation) for the transactions. 我正在使用Hibernate 3.6(JPA 2.0实现)进行交易。 Should I use JTA which is container managed transaction or is it overkilled to use it? 我应该使用JTA这是容器管理的交易还是过度使用它?

Currently I am using JTA and it turns out it is running fine but with some small issues which I do not know whether it is related to the transaction management or not. 目前我正在使用JTA,事实证明它运行正常但有一些小问题,我不知道它是否与事务管理有关。 Will it be easier or more reliable to use local transaction management? 使用本地事务管理会更容易还是更可靠?

JTA transactions are always recommended above other kinds of transaction APIs, especially if you are referring to the native transactions that are still part of the JPA API. 始终建议JTA事务高于其他类型的事务API,特别是如果您指的是仍然属于JPA API的本机事务。 Note that you can't say 'JTA vs resource local transactions', as JTA actually manages resource local transactions among others. 请注意,您不能说“JTA与资源本地事务”,因为JTA实际上管理资源本地事务等。

Gavin King (creator of Hibernate) once indicated in an interview that this JPA specific API was a mistake and that the much more flexible JTA API should be preferred. Gavin King(Hibernate的创建者)曾在一次采访中表示,这个特定于JPA的API是一个错误,应该首选更灵活的JTA API。 Especially when using declarative transactions, JTA is very lightweight. 特别是在使用声明式事务时,JTA非常轻量级。 The word overkill would actually apply more to using the JPA native transaction API, then to using JTA. overkill这个词实际上更多地适用于使用JPA本机事务API,然后使用JTA。

There is something to say about the choice between using XA or resource local transactions with JTA. 关于使用XA或资源本地事务与JTA之间的选择,有一些话要说。 See my answer here for some more details about that: JTA or LOCAL transactions in JPA2+Hibernate 3.6.0? 有关这方面的更多详细信息,请参阅我的答案: JPA2 + Hibernate 3.6.0中的JTA或LOCAL事务?

I do wonder why you are using EJB 2 in combination with JPA 2.0. 我想知道为什么将EJB 2与JPA 2.0结合使用。 EJB 3.1 would be a much more logical choice here. EJB 3.1在这里将是一个更合乎逻辑的选择。 EJB 2 is completely deprecated (will be pruned in Java EE 7). EJB 2已完全弃用(将在Java EE 7中进行修剪)。

I would recommend to use XA transaction, even if the application currently accesses only one resource (the database). 我建议使用XA事务,即使应用程序当前只访问一个资源(数据库)。 Resons: Resons:

1) In future, if the application decides to include some other transactional resource apart from the current database, it will find it easier as the XA transaction management is already in place and the multiple transactional resources can then be combined into a single transaction. 1)将来,如果应用程序决定在当前数据库之外包含一些其他事务资源,则会发现它更容易,因为XA事务管理已经到位,并且多个事务资源可以组合成单个事务。

2) As you have a single transactional resource currently, I don't think performance would suffer with use of XA compared to local transaction. 2)由于您目前拥有单一的交易资源,我认为与本地交易相比,使用XA时性能不会受到影响。 The reason is that XA/JTA transaction managers already do some kind of optimization for cases of single transactional resource (they call it one-phase optimization). 原因是XA / JTA事务管理器已经对单个事务资源的情况进行了某种优化(他们称之为单阶段优化)。

Hope that helps. 希望有所帮助。

Nitin 尼廷

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

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