问题描述
我目前在理解JPA概念时遇到了问题。
I am currently having a problem with understanding a concept of JPA.
我目前正在使用/开发最新的EclipseLink,Glassfish,Derby数据库来演示项目。
I am currently using/developing recent EclipseLink, Glassfish, Derby database to demonstrate a project.
在我开发更大的图片之前,我需要绝对确定这个PersistingUnit如何在不同的范围内工作。
Before I develop something in much bigger picture, I need to be absolutely sure of how this PersistingUnit work in terms of different scopes.
我有一堆servlet 3.0,目前在request.session对象中保存用户的相关实体类(同一个war文件中的所有内容)。我目前正在使用EntityManagerFactory和UserTransaction注入的应用程序管理的EntityManager。它在我自己测试时运行顺畅。当2个人同时访问相同的实体时,会发生不同版本的实体。我希望使用托管bean跨越相同的WAR,如果可能的话,使用相同的持久性单元。
I have bunch of servlets 3.0 and currently saving user's associated entity classes in the request.session object (everything in the same war file). I am currently using Application-managed EntityManager using EntityManagerFactory and UserTransaction injection. It works smooth when it is tested by myself. The different versions of entities occur when 2 people accessing the same entities at the same time. I want to work with managed beans cross the same WAR, same persistence unit if possible.
我读过以及那些没有这些范围的解释对我来说有意义。
I have read http://docs.oracle.com/javaee/6/tutorial/doc/bnbqw.html and bunch of explanations of those scopes which don't make sense at all for me.
长话短说,应用和容器管理的EntityManagers有什么用途和区别?
Long story short, what are the usage and difference of app and container managed EntityManagers?
推荐答案
当你说应用程序管理的事务时,它意味着它应该处理事务的代码。简而言之,它意味着:
When you say application managed transaction it means its your code which is supposed to handle the transaction. In a nutshell it means:
您致电:
entityManager.getTransaction().begin(); //to start a transaction
然后如果成功,你将确保调用
then if success you will ensure to call
entityManager.getTranasaction().commit(); //to commit changes to database
如果失败,您将确保致电:
or in case of failure you will make sure to call:
entityManager.getTransaction().rollBack();
现在假设你有一个容器,它知道何时调用 begin()
, commit()
或 rollback()
,这就是容器管理的事务。有人代表您处理交易。
Now imagine you have a container, which knows when to call begin()
, commit()
or rollback()
, thats container managed transaction. Someone taking care of transaction on your behalf.
您只需要指定。
这篇关于应用程序与容器管理的EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!