Java @Resource UserTransaction 和 EntityManager.getTransaction() 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2517062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is difference between @Resource UserTransaction and EntityManager.getTransaction()
提问by TCM
Can anybody explain what is difference between :
任何人都可以解释以下之间的区别:
@Resource
UserTransaction objUserTransaction;
and
和
EntityManager.getTransaction();
And also what is container managed transaction? and how should I do it in my session facade if I want to insert three rows in table in transaction.
还有什么是容器管理事务?如果我想在事务中的表中插入三行,我应该如何在会话外观中执行此操作。
采纳答案by ewernli
EJB are transactional components. The transaction can be managed either by the applicaiton server itself (CMT - container-managed transaction), or manually by yourself within the EJB (BMT - bean-managed transaction).
EJB 是事务性组件。事务可以由应用程序服务器本身管理(CMT - 容器管理的事务),也可以由您自己在 EJB 中手动管理(BMT - bean 管理的事务)。
EJB supports distributed transaction through the JTA specification. The distributed transaction is controlled using UserTransaction
, which has methods begin
, commit
, rollback
.
EJB 通过 JTA 规范支持分布式事务。分布式事务使用 控制UserTransaction
,它具有方法begin
、commit
、rollback
。
With CMT, the application server starts, commit and rollback the transaction (according to the transaction annotations) for you and you are not allowed to interfere. This means you must not access the UserTransaction
in this case. However, with BMT, you do that manually and you control the transaction yourself using the UserTransaction
.
使用CMT,应用服务器为您启动、提交和回滚事务(根据事务注释),您不得干预。这意味着UserTransaction
在这种情况下您不得访问。但是,使用 BMT,您可以手动执行此操作,并使用UserTransaction
.
Let's move now to the EntityManager
. A JPA implementation can be used either within an application server or stand-alone. If use in stand-alone, you need to use EntityManage.getTransaction
to demarcate the JDBC transaction yourself. If used within an application server, the EntityManager
cooperated with the JTA distributed transaction manager transparently for you.
现在让我们转到EntityManager
. JPA 实现既可以在应用程序服务器中使用,也可以独立使用。如果单机使用,则需要使用EntityManage.getTransaction
自己来划分JDBC事务。如果在应用服务器中使用,它会EntityManager
与 JTA 分布式事务管理器对您透明地协作。
Most of the time, you use CMT with @Required
annotation on the EJB. This means that you don't need to access neither UserTransaction
nor EntityManager.getTransaction
. The app. server starts and commits the transaction, but also takes care to rollback if an exception is raised. This is what I would recommend for your facade.
大多数时候,您@Required
在 EJB 上使用带有注释的CMT 。这意味着您既不需要访问UserTransaction
也不需要访问EntityManager.getTransaction
. 应用程序。服务器启动并提交事务,但如果引发异常,也会注意回滚。这就是我为你的门面推荐的。
(There are more subtleties, such as the PersistenceContextType
or the manual enlistment of the entity manager in distributed transaction with EntityManager.joinTransaction
, but that's only if you use the technologies in a different ways as the default).
(还有更多微妙之处,例如PersistenceContextType
在分布式事务中使用 或手动登记实体管理器EntityManager.joinTransaction
,但前提是您以不同方式使用这些技术作为默认值)。
回答by Marco
UserTransaction
refers to the JTAtransaction entity. You will only be able to use this when there is a JTA module available in the application server: for example, if you're deploying an application with this on Tomcat (which by default does not support JTA), code relying on this will fail. This is default type of transaction used in EJB's and MDB's.
UserTransaction
指的是JTA事务实体。只有在应用程序服务器中有可用的 JTA 模块时,您才能使用它:例如,如果您在 Tomcat(默认情况下不支持 JTA)上使用它部署应用程序,则依赖于它的代码将失败. 这是 EJB 和 MDB 中使用的默认事务类型。
EntityManager.getTransaction()
retrieves a localtransaction entity. This also sometimes known as a resource local transaction.
EntityManager.getTransaction()
检索本地事务实体。这有时也称为资源本地事务。
Resource local transactions are very different from JTA transactions: among other things, resource local transactions are specific to a resource, whereas JTA transactions tend to be specific to a particular thread.
资源本地事务与 JTA 事务非常不同:其中,资源本地事务特定于资源,而 JTA 事务往往特定于特定线程。
For more information on the difference between resource local and JTA transactions, see this stackoverflow answer here: What is the difference between a JTA and a local transaction?
有关资源本地事务和 JTA 事务之间差异的更多信息,请参阅此处的 stackoverflow 答案:JTA 和本地事务之间的区别是什么?
回答by Archimedes Trajano
In addition to @Marco's answer which does well to tell the difference between the JTA and resource local transactions.
除了@Marco 的回答,它很好地说明了 JTA 和资源本地事务之间的区别。
Container Managed Transactions are [as it is named] managed by the container rather than your application. This is done through the EJB tier where you just need to write your method and the container will wrap the method around a transaction context so if any part of your method or its lower level calls throws an exception, the transaction will rollback.
容器管理事务 [顾名思义] 由容器而不是您的应用程序管理。这是通过 EJB 层完成的,您只需要在其中编写您的方法,容器将围绕事务上下文包装该方法,因此如果您的方法的任何部分或其较低级别的调用抛出异常,则事务将回滚。
It can also be fine tuned using annotations. More info can be found here https://docs.oracle.com/javaee/5/tutorial/doc/bncij.html
它也可以使用注释进行微调。更多信息可以在这里找到https://docs.oracle.com/javaee/5/tutorial/doc/bncij.html
Note that this is only done through EJBs, and entity managers that are injected on the web tier (e.g. servlets or REST API) do not get managed by the container in which case you have to look up the transaction using @Resource UserTransaction
or EntityManager.getTransaction
, begin()
and commit()
yourself.
请注意,这只是通过EJB的完成,实体是在Web层注入(如servlet或REST API)管理者不要被这种情况下,你必须查找使用事务容器管理,@Resource UserTransaction
或者EntityManager.getTransaction
,begin()
和commit()
你自己。
From Java EE 6 you are allowed to have EJBs inside the web tier so you don't need to have an overly complex project layout unless you start wanting to expose your EJBs as web services.
从 Java EE 6 开始,您可以在 Web 层内使用 EJB,因此您不需要具有过于复杂的项目布局,除非您开始希望将 EJB 公开为 Web 服务。