应用程序与容器管理的 EntityManager
我目前在理解 JPA 概念方面遇到问题。
我目前正在使用/开发最新的 EclipseLink、Glassfish、Derby 数据库来演示一个项目。
在我开发更大范围的东西之前,我需要绝对确定这个 PersistingUnit 在不同范围内如何工作。
我有一堆 servlet 3.0,当前将用户的关联实体类保存在 request.session 对象中(所有内容都在同一个 war 文件中)。我目前正在使用应用程序管理的 EntityManager,使用 EntityManagerFactory 和 UserTransaction 注入。经本人测试,运行顺利。当两个人同时访问同一实体时,就会出现不同版本的实体。如果可能的话,我想使用跨相同 WAR、相同持久性单元的托管 bean。
我已阅读 http://docs.oracle.com/javaee/6/tutorial/doc/bnbqw .html 以及对这些范围的一堆解释对我来说根本没有意义。
长话短说,应用程序和容器管理的 EntityManager 的用法和区别是什么?
I am currently having a problem with understanding a concept of JPA.
I am currently using/developing recent EclipseLink, Glassfish, Derby database to demonstrate a project.
Before I develop something in much bigger picture, I need to be absolutely sure of how this PersistingUnit work in terms of different scopes.
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.
Long story short, what are the usage and difference of app and container managed EntityManagers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您说应用程序管理事务时,这意味着您的代码应该处理该事务。简而言之,这意味着:
您调用:
然后如果成功,您将确保调用
,或者如果失败,您将确保调用:
现在想象您有一个容器,它知道何时调用
begin()
、commit()
或rollback()
,这是容器管理的事务。有人代表您处理交易。您只需要指定这一点。
When you say application managed transaction it means its your code which is supposed to handle the transaction. In a nutshell it means:
You call:
then if success you will ensure to call
or in case of failure you will make sure to call:
Now imagine you have a container, which knows when to call
begin()
,commit()
orrollback()
, thats container managed transaction. Someone taking care of transaction on your behalf.You just need to specify that.
容器管理事务(CMT)可以被视为一种声明式事务,在这种情况下,事务管理被委托给容器(通常是EJB容器),并且可以简化许多开发工作。
如果我们处于具有EJB容器的Java EE环境中,我们可以直接使用CMT。
如果我们在Java SE环境,或者没有EJB容器的Java EE环境中,我们仍然可以利用CMT,一种方法是使用Spring,它使用AOP来实现声明式事务管理;另一种方法是使用 Guice,它使用 PersistFilter 来实现声明式事务。
在 CMT 中,容器(无论是 EJB 容器、Spring 还是 Guice)将负责事务传播和提交/回滚工作;
应用程序管理事务 (AMT) 与 CMT 的不同之处在于我们需要在代码中以编程方式处理事务。
Container managed transaction(CMT) could be regarded as a kind of declarative transaction, in which case, transaction management is delegated to container (normally EJB container), and much development work could be simplified.
If we are in a Java EE environment with an EJB container, we could use CMT directly.
If we are in a Java SE environment, or a Java EE environment without an EJB container, we could still take advantage of CMT, one way is to use Spring, which uses AOP to implement declarative transaction management; Another way is to use Guice, which uses a PersistFilter to implement declarative transaction.
In CMT, a container (whatever an EJB container, Spring or Guice) will take care of the transaction propagation and commit/rollback stuff;
Application managed transaction (AMT) differs from CMT in that we need to handle transactions programmatically in our code.