简体   繁体   English

容器管理的交易

[英]Container-Managed Transactions

Just a clarification on my understanding of how Container-Managed Transactions(CMTs) work in JPA - 请澄清一下我对JPA中容器管理的交易(CMT)的工作方式的理解-

CMTs spare the application the effort of beginning and committing transactions explicitly right? CMT可以使应用程序省去启动和提交事务的工作,对吗?

CMTs can ONLY be applied to session and mesage-driven beans and NOT pojos? CMT只能应用于会话和消息驱动的bean,不能应用于pojos?

My rationale for the above questions is - i'd like to know how to access an entity from a java-se application as well as a java-ee. 对于上述问题,我的理由是-我想知道如何从java-se应用程序以及java-ee访问实体。 Do i need two separate persistence unit? 我需要两个单独的持久性单元吗?

I allow myself to rewrite completely my answer cause it wasn't clear at all and worse, some things were simply wrongs. 我允许自己完全重写我的答案,因为它根本不清楚,更糟的是,有些事情简直是错误的。

The fact is that you (and I) are mixing EJB and JPA terminology. 事实是您(和我)正在混合使用EJB和JPA术语。
JPA is only talking about entity beans. JPA只在谈论实体bean。 Session bean (including CMT and BMT) are part of the EJB spec. 会话bean(包括CMT和BMT)是EJB规范的一部分。
In JPA we will talk about container-managed and application-managed entity manager linked to JTA or resource-local persitence unit. 在JPA中,我们将讨论链接到JTA或资源本地持久性单元的容器管理和应用程序管理的实体管理器。

Here are the relevant part of the JPA spec : 这是JPA规范的相关部分:

A container-managed entity manager must be a JTA entity manager. 容器管理的实体管理器必须是JTA实体管理器。 JTA entity managers are only specified for use in Java EE containers. JTA实体管理器仅指定用于Java EE容器。 An application-managed entity manager may be either a JTA entity manager or a resource-local entity manager. 应用程序管理的实体管理器可以是JTA实体管理器,也可以是资源本地实体管理器。

[...] [...]

Both JTA entity managers and resource-local entity managers are required to be supported in Java EE web containers and EJB containers. Java EE Web容器和EJB容器均需要同时支持JTA实体管理器和资源本地实体管理器。 Within an EJB environment, a JTA entity manager is typically used. 在EJB环境中,通常使用JTA实体管理器。 In general, in Java SE environments only resource-local entity managers are supported. 通常,在Java SE环境中,仅支持资源本地实体管理器。

[...] [...]

An entity manager whose transactions are controlled through JTA is a JTA entity manager. 通过JTA控制事务的实体管理器是JTA实体管理器。 A JTA entity manager participates in the current JTA transaction, which is begun and committed external to the entity manager and propagated to the underlying resource manager.` JTA实体管理器参与当前的JTA事务,该事务在实体管理器外部开始并提交,并传播到基础资源管理器。

[...] [...]

When a container-managed entity manager is used, the lifecycle of the persistence context is always managed automatically, transparently to the application, and the persistence context is propagated with the JTA transaction 使用容器管理的实体管理器时,始终自动对应用程序透明地管理持久性上下文的生命周期,并且持久性上下文随JTA事务传播

So you will need to define 2 persistence unit only if you want to use a JTA (weather or not container-managed) entity manager in the java EE application. 因此,仅当您想在Java EE应用程序中使用JTA(天气或非容器管理的)实体管理器时,才需要定义2个持久性单元。

CMT is defined declaratively using annotations that are evaluated by the Java EE container which will then provide the required transaction handling transparently. CMT是使用Java EE容器评估的注释以声明方式定义的,然后将透明地提供所需的事务处理。 Pojos are not managed by a container, so no CMT can be applied. Pojos不受容器管理,因此无法应用CMT。

As for your question about the entities. 至于关于实体的问题。 You should create a DAO layer to abstract away the technical details of your persistence logic. 您应该创建一个DAO层来抽象化持久性逻辑的技术细节。 you can basically use one generic dao implementation to support JPA. 您基本上可以使用一种通用的dao实现来支持JPA。 This basically the only part that needs to be different for thw two environments. 这基本上是两个环境中唯一需要不同的部分。 In a container you will get your transactions for free as defined in the annotations. 在容器中,您将免费获得注释中定义的交易。 If running in standard java se, you must begin/commit/rollback your transactions yourself. 如果运行在标准Java SE中,则必须自己开始/提交/回滚事务。

I suggest that you create a generic dao implementation that defines transactions declaratively and expects to run inside a container. 我建议您创建一个通用的dao实现,该实现以声明方式定义事务并希望在容器内运行。 For use in java se you have a decorator for this dao that takes care of proper transaction handling to emulate what the container would actually do. 为了在Java SE中使用,您需要为此dao装饰器,该装饰器负责适当的事务处理以模仿容器实际执行的操作。

I think you don't really need to change anything in the persistence.xml but maybe I am wrong here 我认为您实际上不需要更改persistence.xml中的任何内容,但也许我在这里错了

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM