@Resource UserTransaction 和 EntityManager.getTransaction() 有什么区别 [英] What is difference between @Resource UserTransaction and EntityManager.getTransaction()

查看:27
本文介绍了@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<控制分布式事务/code>,其中包含方法 begincommitrollback.

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.这意味着您不需要访问 UserTransactionEntityManager.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() 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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