UserTransaction和EntityTransaction之间的区别 [英] Difference between UserTransaction and EntityTransaction

查看:284
本文介绍了UserTransaction和EntityTransaction之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了一切: UserTransaction EntityTransaction 之间有什么区别?

Title says it all: What is the difference between a UserTransaction and an EntityTransaction?

我的基本理解是当需要JTA时使用 UserTransaction (例如,对多项内容进行查询),以及 EntityTransaction 仅在需要JPA时使用(例如,当查询是原子的时候)。

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?

推荐答案


我的基本理解是
当JTA需要
时使用UserTransaction(例如,对
多个事物进行查询),并且仅在需要JPA
时使用
EntityTransaction(例如,当查询$ b时$ b是原子的。

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).

这基本上是对的,但你对多重事物和原子的描述有点奇怪。 JTA允许开发人员使用分布式事务以原子方式(全有或全无)对多个资源(数据库,JMS代理等)执行更改。如果只访问一个资源(例如一个数据库),则不需要JTA,但事务仍然是原子的(全有或全无)。例如,当您在一个数据库上使用常规JDBC事务时就是这种情况。

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.

考虑 UserTransaction EntityTransaction


  • 如果JPA是独立使用,则使用 EntityTransaction 自行划分事务。

  • 如果在与JTA集成的托管环境中使用JPA,则使用 UserTransaction EntityManager 将自身挂钩到JTA分布式事务管理器中。我所知道的唯一微妙之处就是对变化的反映。当使用 EntityTransaction 时,JPA知道它需要刷新更改。如果使用 UserTransaction 控制事务,则需要使用JTA注册回调 registerSynchronization ,以便在事务完成之前,更改将刷新到数据库。如果将EJB与CMT(容器管理事务)一起使用,则甚至不需要使用 UserTransaction :应用服务器为您启动和停止事务。

  • 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. The EntityManager hooks itself into the JTA distributed transaction manager. The only subtlety I'm aware of considers the flush of the changes. When EntityTransaction is used, JPA know it needs to flush the changes. If transaction are controlled using UserTransaction, it needs to register a callback using JTA registerSynchronization, 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 use UserTransaction: the app server starts and stops the transactions for you.

相关问题:

  • What is difference between UserTransaction and EntityManager.getTransaction() (duplicate)
  • XA and non-XA datasource with Spring/Hibernate (related)

这篇关于UserTransaction和EntityTransaction之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆