UserTransaction和EntityManager如何交互? [英] How does the UserTransaction and the EntityManager interact?

查看:102
本文介绍了UserTransaction和EntityManager如何交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个学术问题;我没有与此有关的坏代码.我只是想加深对引擎盖下正在发生的事情的理解.

This is an academic question; I have no broken code in relation to this. I just want to expand my understanding of what is happening under the hood.

我在JPA DAO中为典型的JSF Web应用程序使用的代码模式(从书本和教程中复制)基本上是这样的:

The code pattern I have been using (copied from books and tutorials) in my JPA DAO for my typical JSF web applications is basically this:

public class someDAO {

    @PersistenceContext protected EntityManager   em;
    @Resource           private   UserTransaction utx;    

    public void persist(Entity entity) {
        try {
            utx.begin();
            em.persist(entity);
            utx.commit();
        } catch ( // gawd awful long list of possible exceptions ) 

        // etc

所以我的问题如下:

  1. 为什么从两个看似无关的程序包中注入EntityManager实例和UserTransaction实例注解?

  1. Why are the EntityManager instance and the UserTransaction instance injected with annotations from two seemingly unrelated packages?

为什么使用注释@Resource和@PersistenceContext而不是@ManagedProperty或@Inject?

Why is the annotation @Resource and @PersistenceContext rather than @ManagedProperty or perhaps @Inject used?

persist()方法如何访问 utx 对象并与之交互?如果我忘记了 utx.begin()调用,则实体管理器会知道并抛出异常.它必须以某种神奇的方式找到UserTransaction对象.定义这样的接口不是更好的架构吗: em.persist(utx,entity)?

How does the persist() method access and interact with the utx object? If I forget the utx.begin() call the entity manager knows about it and throws and exception. It must be finding the UserTransaction object in some magic way. Wouldn't it have been better architecture to define the interface like: em.persist(utx, entity)?

如果 utx 是某种单例-一次可以打开多个UserTransaction吗?

If utx is some sort of singleton -- is it possible to have more than one UserTransaction open at a time?

非常感谢您的讨论.

推荐答案

  1. 因为UserTransaction是Java Transaction API(JTA)的一部分,而EntityManager是Java Persistence API(JPA)的一部分. JTA不属于JPA. JPA使用JTA提供的服务.

  1. Because UserTransaction is part of Java Transaction API (JTA) and EntityManager is part of Java Persistence API (JPA). JTA is not part of JPA. JPA uses services provided by JTA.

不是ManagedProperty注释,仅在使用@ManagedBean注释的类中有效.也许最好不要注入UserTransaction 托管Bean中使用不同的方式.

Isn't ManagedProperty is some annotation which is valid only in classes annotated with @ManagedBean. Maybe it was considered better to not inject UserTransaction different way in managed beans.

JNDI查找活动交易.保留名称似乎是java:comp/UserTransaction.一种实现: http://www .java2s.com/Open-Source/Java-Document/Database-ORM/hibernate/org/hibernate/transaction/JTATransactionFactory.java.htm

JNDI lookup for active transaction. Reserved name seems to be java:comp/UserTransaction. One implementation: http://www.java2s.com/Open-Source/Java-Document/Database-ORM/hibernate/org/hibernate/transaction/JTATransactionFactory.java.htm

这不是某种单例,您可以有多个.但是每个线程只能激活一个.

It is not some sort of singleton, you can have more than one. But only one per thread can be active.

这篇关于UserTransaction和EntityManager如何交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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