understand EntityManager.joinTransaction()

1.示例

@Stateful
public class ShoppingCartImpl implements ShoppingCart {
    @PersistenceUnit
    private EntityManagerFactory emf;
    private EntityManager em;
    private Order order;
    private Product product;
    @PostConstruct
    public void init() {
        em = emf.createEntityManager();
    }
    public void initOrder(Long id) {
        order = em.find(Order.class, id);
    }
    public void initProduct(String name) {
        product = (Product) em.createQuery("select p from Product p
                where p.name = :name")
                .setParameter("name", name)
                .getSingleResult();
    }
    public LineItem createLineItem(int quantity) {
        em.joinTransaction();
        LineItem li = new LineItem(order, product, quantity);
        order.getLineItems().add(li);
        em.persist(li);
        return li;
    }
    @Remove
    public void destroy() {
        em.close();
    }
}

2.解析

First, a few words of theory...

An application-managed entity manager participates in a JTA transaction in one of two ways.

1. If the persistence context is created inside the transaction, the persistence provider will automatically synchronize

the persistence context with the transaction.
2.If the persistence context was created earlier (outside of a transaction or in a transaction that has since ended), the

persistence context can be manually synchronized with the transaction by calling joinTransaction() on the EntityManager

interface. Once synchronized, the persistence context will automatically be flushed when the transaction commits.

After reading the above definition a few questions may arise:

1.how do we know that ShoppingCartImpl participates in JTA transaction ?

  Because the class has been annotated with @Stateful (or @Stateless) annotation so the intention is to execute the class

within Java EE environment which by default uses JTA transactions. A class doesn‘t need such annotation, if it will be executed

in Java SE environment.

2.how do we know application-managed entity manager is used in this particular case?

Because we are using @PersistenceUnit annotation to inject EntityManagerFactory and then manually creating and destroying

EntityManager. By doing this we are telling Java EE container that we don‘t want our transaction to be automatically managed

(like in case of transaction-scoped entity manager or extended entity manager types).

3.why em.joinTransaction() is required in createLineItem method?

By calling em.joinTransaction() we notify the application-managed persistence context that it should synchronize itself with the

current JTA transaction. Without such call the changes to Order would not be flushed to the underlying database when the

transaction commits (at the end of createLineItem method).

NOTE: since EntityManagerFactory instances are thread-safe and EntityManager instances are not, an application must not call

em.joinTransaction() on the same entity manager in multiple concurrent transactions.

引用

http://stackoverflow.com/questions/24442335/use-of-jointransaction-in-jpa

时间: 2025-01-31 07:15:21

understand EntityManager.joinTransaction()的相关文章

javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available..

javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call 今天用spring4.2.5版进行数据更新的时候出现了这个问题,粗略的看报错的应该是声明式事务有点问题,可是和以前用的3.0版本的配置一样,问题出在哪里呢,百度也找不到好的答案. 我们知

understand的安装

1.win7 64位下安装 1)下载Understand.4.0.908.x64.rar. 2)解压之,直接运行里面的Understand-4.0.908-Windows-64bit.exe. 3)选择如下 之后,点击"Add Eval or SDL (RegCode)",如下图: 4)出现如下界面 进入破解程序生成code,如下 将生成的code填入即可,可以不用填写email.ok! 2.Linux下安装understand 为了能方便的看代码,想安装 Scientific Too

Understand the Qt containers(有对应表)

Container classes are one of the cornerstones of object-oriented programming, invaluable tools that free us from having to permanently think about memory management. Qt comes with its own set of container classes, closely modeled after those in the S

EJB3 EntityBean中EntityManager的管理类型

EJB中EntityManager的管理方式有两种:Container-managed EntityManager和Application-managed EntityManager 即容器管理的EntityManager和应用管理的EntityManager 在EJB中,EntityManager所进行的持久化的方式与Hibernate的方式是不同的. 1.在Hibernate的同一个事务中,通过getCurrentSession获取的session对象均为同一个,保存于threadlocal

JPA学习笔记(5)——EntityManager相关

Persistence EntityManagerFactory EntityManager find方法 getReference方法 persist方法 remove方法 merge方法 情况1传入的对象没有id 情况2传入的对象有identityManager的缓存中没有该对象数据库中没有该记录 情况3传入的对象有identityManager的缓存没有该对象数据库中有该记录 情况4传入的对象有identityManager的缓存有该对象 flush方法 refresh方法 clear c

Error: Selected device is not a touchscreen I understand

selected device is not a touchscreen I understand arm交叉编译工具中的头文件库中的linux/input.h中的EV_VERSION定义为 #define EV_VERSION 0x010000 而linux内核include/linux/input.h中的EV_VERSION定义为 #define EV_VERSION 0x010001 由此可见问题就出现在内核的输入子系统的版本号不匹配的问题 解决办法:     1.将内核源代码里的incl

【go语言】wait,I don't understand

该文内容来看读<Go并发编程实战>有感,仅供娱乐分享 :) 在%GOROOT%\src\sort包下有一个sort.go文件,里面第12行有这么一个接口定义: type Interface interface { // Len is the number of elements in the collection. Len() int // Less reports whether the element with // index i should sort before the eleme

EntityManager无法remove entity的问题

今天遇到一个奇怪的事情,利用EntityManager.remove(entity)方法删除一个entity时,删不掉,也不报错.后来经过多方查证,解决了这个问题. ERD Entity定义 ------------- 第一个Entity A --------------- @Entity public class A {     @Id     private Long id;          @Column(nullable = false, unique = true, length =

浅析JPA中EntityManager无法remove entity的问题

今天遇到一个奇怪的事情,利用EntityManager.remove(entity)方法删除一个entity时,删不掉,也不报错.后来经过多方查证,解决了这个问题. ERD Entity定义 ------------- 第一个Entity A --------------- @Entity public class A {     @Id     private Long id;          @Column(nullable = false, unique = true, length =