简体   繁体   English

应用程序与容器管理的EntityManager

[英]Application vs Container Managed EntityManager

I am currently having a problem with understanding a concept of JPA. 我目前在理解JPA的概念时遇到了问题。

I am currently using/developing recent EclipseLink, Glassfish, Derby database to demonstrate a project. 我目前正在使用/开发最近的EclipseLink,Glassfish,Derby数据库来演示一个项目。

Before I develop something in much bigger picture, I need to be absolutely sure of how this PersistingUnit work in terms of different scopes. 在我开发更大的图片之前,我需要绝对确定这个PersistingUnit如何在不同的范围内工作。

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). 我有一堆servlet 3.0,目前在request.session对象中保存用户的相关实体类(同一个war文件中的所有内容)。 I am currently using Application-managed EntityManager using EntityManagerFactory and UserTransaction injection. 我目前正在使用EntityManagerFactory和UserTransaction注入的应用程序管理的EntityManager。 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. 当2个人同时访问相同的实体时,会发生不同版本的实体。 I want to work with managed beans cross the same WAR, same persistence unit if possible. 我希望使用托管bean跨越相同的WAR,如果可能的话,使用相同的持久性单元。

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. 我已经阅读了http://docs.oracle.com/javaee/6/tutorial/doc/bnbqw.html以及那些对我来说根本没有意义的范围的解释。

Long story short , what are the usage and difference of app and container managed EntityManagers? 简而言之 ,应用程序和容器管理的EntityManagers的用途和区别是什么?

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: 你打电话:

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();

Now imagine you have a container, which knows when to call begin() , commit() or rollback() , thats container managed transaction. 现在假设你有一个容器,它知道何时调用begin()commit()rollback() ,这就是容器管理的事务。 Someone taking care of transaction on your behalf. 有人代表您处理交易。

You just need to specify that. 你只需要指定它。

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. 容器管理事务(CMT)可以被视为一种声明性事务,在这种情况下,事务管理被委托给容器(通常是EJB容器),并且可以简化许多开发工作。

If we are in a Java EE environment with an EJB container, we could use CMT directly. 如果我们在具有EJB容器的Java EE环境中,我们可以直接使用CMT。

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; 如果我们在Java SE环境或没有EJB容器的Java EE环境中,我们仍然可以利用CMT,一种方法是使用Spring,它使用AOP来实现声明式事务管理; Another way is to use Guice, which uses a PersistFilter to implement declarative transaction. 另一种方法是使用Guice,它使用PersistFilter来实现声明式事务。

In CMT, a container (whatever an EJB container, Spring or Guice) will take care of the transaction propagation and commit/rollback stuff; 在CMT中,容器(无论是EJB容器,Spring还是Guice)都将处理事务传播和提交/回滚内容;

Application managed transaction (AMT) differs from CMT in that we need to handle transactions programmatically in our code. 应用程序管理事务(AMT)与CMT的不同之处在于我们需要在代码中以编程方式处理事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在 bean 中组合容器管理和应用程序管理的实体管理器? - Is it possible to combine a container managed and application managed entitymanager in a bean? 使用容器管理的EntityManager进行会话管理 - Session management with a container managed EntityManager JPA 2.0 /托管的应用程序/ EntityManager为null - JPA 2.0 / Application managed / EntityManager null 在EJB容器上的CDI EntityManager注入VS @PersistenceContext实体管理器 - CDI EntityManager injection VS @PersistenceContext entitymanager on an EJB container TomEE + OpenJPA:使用容器管理的数据源创建EntityManager时出错 - TomEE + OpenJPA: Error creating EntityManager using container managed DataSource 容器管理的EntityManager:单个持久性单元的多个管理器? - Container-managed EntityManager: multiple managers for single persistence unit? Web APplication的容器管理安全性 - Container Managed Security for Web APplication Guice提供者 <EntityManager> vs EntityManager - Guice Provider<EntityManager> vs EntityManager EJB 3.1容器管理并发与同步 - EJB 3.1 container managed concurrency vs. synchronized Tapestry `EntityManager` 与 `EntityManagerManager` - Tapestry `EntityManager` vs `EntityManagerManager`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM