understand EntityManager.joinTransaction()
Join Transaction
The EntityManager.joinTransaction()
API allows an application managed EntityManager
to join the active JTA transaction context. This allows an EntityManager
to be created outside the JTA transaction scope and commit its changes as part of the current transaction. This is normally used with a stateful SessionBean
, or with a JSP or Servlet where an EXTENDED EntityManager
is used in a stateful architecture. A stateful architecture is one where the server stores information on a client connection until the client's session is over, it differs from a stateless architecture where nothing is stored on the server in between client requests (each request is processed on its own).
There are pros and cons with both stateful and stateless architectures. One of the advantages with using a stateful architecture and and EXTENDED EntityManager
, is that you do not have to worry about merging objects. You can read your objects in one request, let the client modify them, and then commit them as part of a new transaction. This is where joinTransaction
would be used. One issue with this is that you normally want to create the EntityManager
when there is no active JTA transaction, otherwise it will commit as part of that transaction. However, even if it does commit, you can still continue to use it and join a future transaction. You do have to avoid using transactional API such as merge
or remove
until you are ready to commit the transaction.
joinTransaction
is only used with JTA managed EntityManager
s (JTA transaction-type in persistence.xml). For RESOURCE_LOCAL
EntityManager
s you can just commit the JPA transaction whenever you desire.
Example joinTransaction usage
EntityManager em = getEntityManagerFactory().createEntityManager();
Employee employee = em.find(Employee.class, id);
employee.setSalary(employee.getSalary() + 1000); UserTransaction transaction = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");
transaction.begin();
em.joinTransaction();
transaction.commit();
参考:https://en.wikibooks.org/wiki/Java_Persistence/Transactions
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
understand EntityManager.joinTransaction()的更多相关文章
- Utility3:Understand Dashboard Report
To see data in the SQL Server Utility dashboard, select the top node in the Utility Explorer tree - ...
- JPA中entityManager的CRUD
private EntityManagerFactory entityManagerFactory; private EntityManager entityManager; private Enti ...
- 代码阅读分析工具Understand 2.0试用
Understand 2.0是一款源代码阅读分析软件,功能强大.试用过一段时间后,感觉相当不错,确实可以大大提高代码阅读效率.由于Understand功能十分强大,本文不可能详尽地介绍它的所有功能,所 ...
- 【转载】Understand the serialVersionUID
If you have ever implemented Serializable interface, you must encounter this warning message The ser ...
- understand一些功能
功能 支持分析的语言 统计总的代码数据 统计单个文件的数据 分析代码复杂度 分析代码格式 文件的依赖关系 文件夹依赖关系 文件夹包含关系.代码量 understand提供了很多图表,同时它可以根据源码 ...
- Understand:高效代码静态分析神器详解(转)
之前用Windows系统,一直用source insight查看代码非常方便,但是年前换到mac下面,虽说很多东西都方便了,但是却没有了静态代码分析工具,很幸运,前段时间找到一款比source ins ...
- Five More Hacker Tools Every CISO Should Understand
As we mentioned in the first article, Top Five Hacker Tools Every CISO Should Understand, the role o ...
- Top Five Hacker Tools Every CISO Should Understand
As the role of the CISO continues to evolve within organizations towards that of an executive level ...
- 如何获取EntityManager
1.在容器内部使用,使用@PersistenceContext 来注入.@PersistenceContextprivate EntityManager em;TAG================= ...
随机推荐
- Groovy学习()起步
安装Groovy 在Windows环境下安装Groovy 下载安装包,傻瓜安装,设置环境目录,就这样搞定. 另外还需要安装Java并且设置JAVA_HOME环境变量. 两分钟搞定. 使用groovys ...
- mysql索引学习
索引用于快速找出在某列中有一特定值的行. 如果不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行. 表越大,查询数据所花费的时间越多. 如果表中查询的列有一个索引,MySQL能快速 ...
- CSS——伪元素
CSS伪元素 伪元素的语法: selector:pseudo-element {property:value;} 作 用 添加特殊样式 :first-line 伪元素 "first-l ...
- asp.net 正在加载/处理(兼容IE Chrome)
正在加载分两种 一种是页面初始化效果,也就是数据读取前,这个时候需要用ajax先显示读取中数据,当ajax回调的时候,读取页面数据 一种是界面读取中效果,就是用div显示,当OnPreRenderCo ...
- 29个酷炫的Firefox配置参数
你可能安装了许多的firefox插件以增加浏览器的功能,但是如果你想更好地使用firefox,学习如何配置about:config参数是很有必要的. about:config配置页包含了所有的fire ...
- HTML5关于上传API的一些使用(上)
HTML5提供了很多有用的API,其中就包括上传的API,XMLHttpRequest2.0,在HTML5时代之前,需要进行二进制的上传一般都会才用flash的方案,但是当XMLHttpRequest ...
- alert的美化
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 使ie9以下版本支持canvas,css3等主流html5技术的方法
1.前言. ie6,7,8支持html5,看起来比较难,其实有一种方法很通用,就是引入js和css,这种可插拔的引入对开发很有帮助.比如,下面是一个让网页支持canvas和css3的例子. 2.例 ...
- javax.Servlet的包中,属于类的是。(选择1项)
javax.Servlet的包中,属于类的是.(选择1项) A.Servlet B.GenericServlet C.ServletRequest D.ServletContext 解答:B Serv ...
- Angular入门篇高速开发导航网
简单介绍 AngularJS 是一个为动态WEB应用设计的结构框架,提供给大家一种新的开发应用方式.这样的方式能够让你扩展HTML的语法.以弥补在构建动态WEB应用时静态文本的不足.从而在web应用程 ...