Container-managed operations

Just an explanation of my understanding of how container-managed transactions (CMTs) work in JPA -

CMT save the application from trying to start and complete transactions directly?

Can CMT be applied ONLY to session and managed mesage beans and NOT pojos?

My rationale for the above questions is that I would like to know how to access the object from the java-se application, as well as java-ee. Do I need two separate save blocks?

+2
source share
2 answers

I allow myself to completely rewrite my answer, because it was not entirely clear and worse, some things were just mistakes.

The fact is that you (and I) mix EJB and JPA terminology.
JPA talks only about the beans object. The bean session (including CMT and BMT) is part of the EJB specification.
In JPA, weโ€™ll talk about the manager of entities managed by containers and applications associated with a JTA or resource localization node.

Here is the relevant part of the JPA specification:

The container-managed object manager must be a JTA entity manager. JTA entity administrators are specified for use in Java EE containers only. An application-driven object manager can be either a JTA entity manager or a local-resource object manager.

[...]

Both JTA entity administrators and local object administrators must be supported in Java EE web containers and EJB containers. In an EJB, the JTA object manager is typically used. In general, in Java SE environments, only local entity manager resources are supported.

[...]

The manager of entities whose transactions are controlled through the JTA is the JTA entity manager. The JTA unit manager is involved in the current JTA transaction, which begins and takes place outside the facility manager and is transferred to the primary resource manager.

[...]

When a court-managed container management object is used, the persistence context life cycle is always automatically controlled, transparent to the application, and the persistence context is propagated by the JTA transaction

Thus, you will need to define 2 units of continuity only if you want to use the JTA entity manager (weather-driven or not-container-managed) in the Java EE application.

+4
source

CMT is declaratively defined using annotations that are evaluated by the Java EE container, which then provides transparent transaction processing. Pojos is not container managed, so CMT cannot be applied.

Regarding your question about objects. You must create a DAO layer to abstract the technical details of your persistence logic. you can use one common dao implementation to support JPA. This is basically the only part that should be different for the two environments. In the container, you will receive your transactions for free, as indicated in the annotation. If you are working in standard java se, you must start / commit / roll back your transactions yourself.

I suggest you create a generic dao implementation that declares transactions and expects it to be launched inside the container. For use in java se, you have a decorator for this dao that takes care of the proper transaction processing to emulate what the container would actually do.

I think you do not need to change anything in the persistence.xml file, but maybe I'm wrong here.

+1
source

All Articles