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

编程入门 行业动态 更新时间:2024-10-28 19:32:52
本文介绍了@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,其中包含方法 begin、commit、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:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   UserTransaction   Resource   getTransaction   EntityManager

发布评论

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

>www.elefans.com

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