Java UserTransaction 和 EntityTransaction 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3078245/
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
Difference between UserTransaction and EntityTransaction
提问by bguiz
Title says it all: What is the difference between a UserTransaction
and an EntityTransaction
?
标题说明了一切: aUserTransaction
和 an之间有什么区别EntityTransaction
?
My rudimentary understanding is that UserTransaction
is used when JTA is required (e.g. to do queries on mulitple things), and that EntityTransaction
is used when JPA only is required (e.g. when the query is atomic).
我的基本理解是UserTransaction
在需要 JTA 时使用(例如对多个事物进行查询),而EntityTransaction
在仅需要 JPA 时使用(例如当查询是原子时)。
Is that the only difference between the two or is there more to it than that?
这是两者之间的唯一区别还是除此之外还有更多区别?
采纳答案by ewernli
My rudimentary understanding is that UserTransaction is used when JTA is required (e.g. to do queries on mulitple things), and that EntityTransaction is used when JPA only is required (e.g. when the query is atomic).
我的基本理解是 UserTransaction 在需要 JTA 时使用(例如对多个事物进行查询),而 EntityTransaction 在仅需要 JPA 时使用(例如当查询是原子的时)。
That's basically right, but your description of "multiple things" and "atomic" is a bit strange. JTA allows the developper to use distributed transaction to perform changes on multiples resources (database, JMS broker, etc.) atomically (all-or-nothing). If only one resource is accessed (e.g. one single database), you don't need JTA, but the transaction is still atomic (all-or-nothing). That's for instance the case when you use a regular JDBC transaction on one database.
基本上是对的,但是你对“多事物”和“原子”的描述有点奇怪。JTA 允许开发人员使用分布式事务以原子方式(全有或全无)对多个资源(数据库、JMS 代理等)执行更改。如果只访问一个资源(例如一个数据库),则不需要 JTA,但事务仍然是原子的(全有或全无)。例如,当您在一个数据库上使用常规 JDBC 事务时就是这种情况。
Considering UserTransaction
vs. EntityTransaction
:
考虑UserTransaction
vs. EntityTransaction
:
- If JPA is use stand-alone, you use
EntityTransaction
to demarcate the transaction yourself. - If JPA is used within a managed environment where it integrates with JTA, you use
UserTransaction
. TheEntityManager
hooks itself into the JTA distributed transaction manager. The only subtlety I'm aware of considers the flush of the changes. WhenEntityTransaction
is used, JPA know it needs to flush the changes. If transaction are controlled usingUserTransaction
, it needs to register a callback using JTAregisterSynchronization
, so that the changes are flushed to the database before the transaction completes. If you use EJB with CMT (container managed transaction), you don't even need to useUserTransaction
: the app server starts and stops the transactions for you.
- 如果 JPA 是独立使用的,则使用您
EntityTransaction
自己来划分事务。 - 如果在与 JTA 集成的托管环境中使用 JPA,则使用
UserTransaction
. 将EntityManager
自身挂接到 JTA 分布式事务管理器中。我所知道的唯一微妙之处是考虑了变化的冲洗。何时EntityTransaction
使用,JPA 知道它需要刷新更改。如果使用 控制事务UserTransaction
,则需要使用 JTA 注册回调registerSynchronization
,以便在事务完成之前将更改刷新到数据库。如果您将 EJB 与 CMT(容器管理事务)一起使用,您甚至不需要使用UserTransaction
:应用服务器为您启动和停止事务。
Related questions:
相关问题: