@Resource UserTransaction和EntityManager.getTransaction()之间有什么区别

编程入门 行业动态 更新时间:2024-10-28 21:14:30
本文介绍了@Resource UserTransaction和EntityManager.getTransaction()之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

任何人都能解释一下有什么区别:

Can anybody explain what is difference between :

@Resource UserTransaction objUserTransaction;

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.

推荐答案

EJB是事务性的组件。事务可以由应用程序服务器本身(CMT - 容器管理的事务)管理,也可以由EJB中自己手动管理(BMT - bean管理的事务)。

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通过JTA规范支持分布式事务。使用 UserTransaction ,其方法开始,提交, rollback 。

EJB supports distributed transaction through the JTA specification. The distributed transaction is controlled using UserTransaction, which has methods begin, commit, rollback.

使用CMT,应用程序服务器启动,提交和回滚事务(根据交易注释)为您和您不允许干涉。这意味着在这种情况下,您不能访问 UserTransaction 。但是,使用BMT,您可以手动执行此操作,并使用 UserTransaction 自行控制事务。

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.

现在让我们行动吧到 EntityManager 。 JPA实现可以在应用程序服务器中使用,也可以单独使用。如果在独立使用,则需要使用 EntityManage.getTransaction 自行划分JDBC事务。如果在应用程序服务器中使用, EntityManager 将透明地与JTA分布式事务管理器协作。

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.

大多数情况下,您在EJB上使用带有 @Required 注释的CMT。这意味着您既不需要访问 UserTransaction 也不需要 EntityManager.getTransaction 。该应用程序。服务器启动并提交事务,但如果引发异常,也会小心回滚。这就是我建议您的外观。

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.

(还有更多细微之处,例如 PersistenceContextType 或实体管理员的手动登记使用 EntityManager.joinTransaction ,但仅当您以不同方式使用技术作为默认值时才会这样做。

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

更多推荐

@Resource UserTransaction和EntityManager.getTransaction()之间有什么区别

本文发布于:2023-11-25 16:24:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630477.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   UserTransaction   Resource   getTransaction   EntityManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!