UserTransaction 和 EntityTransaction 之间的区别
标题说明了一切: 之间有什么区别UserTransaction
和 实体事务
?
我的初步理解是,当需要JTA时(例如对多个事物进行查询),使用UserTransaction
,而当仅需要JPA时(例如,当查询是原子的)。
这是两者之间唯一的区别还是还有更多的区别?
Title says it all: What is the difference between a UserTransaction
and 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).
Is that the only difference between the two or is there more to it than that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这基本上是正确的,但是你对“多种事物”和“原子”的描述有点奇怪。 JTA 允许开发人员使用分布式事务以原子方式(全有或全无)对多个资源(数据库、JMS 代理等)执行更改。如果只访问一种资源(例如,一个数据库),则不需要 JTA,但事务仍然是原子的(全有或全无)。例如,当您在一个数据库上使用常规 JDBC 事务时,就会出现这种情况。
考虑
UserTransaction
与EntityTransaction
:EntityTransaction
自行划分事务。UserTransaction
。EntityManager
将自身挂接到 JTA 分布式事务管理器中。我所知道的唯一微妙之处在于变化的冲洗。当使用 EntityTransaction 时,JPA 知道它需要刷新更改。如果使用UserTransaction
控制事务,则需要使用JTA注册回调registerSynchronization
,以便在事务完成之前将更改刷新到数据库。如果您将 EJB 与 CMT(容器管理事务)结合使用,您甚至不需要使用UserTransaction
:应用程序服务器会为您启动和停止事务。相关问题:
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.
Considering
UserTransaction
vs.EntityTransaction
:EntityTransaction
to demarcate the transaction yourself.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.Related questions: