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================= ...
随机推荐
- Java单元测试学习
单元测试的好处 1. 让你写出更好的代码:职业高内聚.低耦合而且接口设计合理的代码才易于测试: 2. 让你在修改代码时更有信心. JUnit4 注解 @Test (expected = Excepti ...
- Unix系统编程()深入探究文件IO概述
open调用将引入原子atomicity操作的概念. 将某一系统调用所要完成的各个动作作为不可中断的操作,一次性加以执行. 原子操作是许多系统调用得以正确执行的必要条件. 还介绍一个系统调用fcntl ...
- java 错误汇总
一.怎么处理警告:编码 GBK 的不可映射字符 解决办法是:应该使用-encoding参数指明编码方式:javac -encoding UTF-8 XX.java,这下没警告了,运行也正确了在JCre ...
- ES6学习笔记(1,let和const)
在介绍let和const之前我们先复习一下相关的知识点. 关于函数作用域 开发过程中,在ES6(ECMA2015)标准推出之前,声明变量的方式一直都是var,而变量的作用域一般也只在函数内部,即函数作 ...
- windows下端口占用解决方法-查看和杀死占用端口进程
在Windows下启动程序时有时会遇到端口被占用的情况,由于一个端口同时只能运行一个进程,所以要想启动新的程序就要先把占用该端口的进程给kill掉,具体的命令分为以下三步, 以杀死占用了80端口的进程 ...
- NodeJS与Javascript时代
如果你一直在关注互联网的相关技术,你应该会有这样一种感觉,web技术正在发生着变革,虽然我们不愿相信,但一个事实已经越来越清晰的摆在了眼前:LAMP组合的时代将要成为历史,在web诞生的二十年间,它影 ...
- 第二百七十五节,MySQL数据库安装和介绍
MySQL数据库安装 一.概述 1.什么是数据库 ? 答:数据的仓库,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Server等 ? 答:他们均是一种 ...
- Apache -- 压力测试工具ab.exe
ab全称ApacheBench是Apache超文本传输协议(HTTP)的性能测试工具.是描绘当前所安装的Apache的执行性能, 主要是显示你安装的Apache每秒可以处理多少个请求Apache自带的 ...
- linux -- 查看ip,路由,dns
查看ip地址:ifconfig 查看gateway:route 查看dns:nm-tool
- 动态向SqlParameter 里添加相应参数
先定义一个List,然后再往List里面添加SqlParameter对象,然后将List转为SqlParameter数组即可 List<SqlParameter> ilistStr = n ...