This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EntityManagers and transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EntityManagers and transactions" Watch "EntityManagers and transactions" New topic
Author

EntityManagers and transactions

Tim Storms
Ranch Hand

Joined: Apr 27, 2006
Posts: 64
Guys,

I'm very comfused about some concepts. I've listed them below and I would like to know whether everything I state is correct. There are also a few questions listed. I would really appreciate your inputs!

CONTAINER-MANAGED TRANSACTION-SCOPED ENTITYMANAGER
- Uses JTA for transaction management (single or extended)
- Created with @PersistenceContext(type=TRANSACTIONAL/EXTENDED)
- The transactional persistence context begins when the EntityManager is invoked in the scope of an active JTA transaction, and there is no persistence context currently associated with the transaction
- The transactional persistence context ends when the JTA transaction commits or rolls back (so entities become detached)
- When there is no transaction available, a new temporary persistence context will be created upon a method invocation (e.g. entityManager.find())
- An extended persistence context can only be used in stateful beans. The extended persistence context exists from the point a stateful bean declared an EntityManager. The extended persistence context is maintained during the entire lifecycle of the bean. This means that all returned entities are always in an attached (managed) state.
- An extended persistence context is closed when the @Remove annotated method has ended

APPLICATION-MANAGED TRANSACTION-SCOPED ENTITYMANAGER
- Uses JTA for transaction management (always extended, so single is not possible??)
- Created with @PersistenceUnit

APPLICATION-MANAGED RESOURCE-LOCAL ENTITYMANAGER
- Uses EntityTransaction for transaction management
- Created with @PersistenceUnit

COMMON PROPERTIES FOR APPLICATON-MANAGED ENTITYMANAGERS
- All application-managed persistence contexted are extended in scope, and therefore may span multiple transactions. An extended persistence context exists from the moment the EntityManager is created and ends when entityManager.close() is called.
- You should call entityManager.close() when you want to release the peristence context. After you've invoked this method, you can only call isOpen() and getTransaction() or you will get an IllegalStateException. If entityManger.close() within an active transaction, the persistence context will remain managed until the transaction completes.
- An extended persistence context obtained from an application-managed EntityManager is a stand-alone persistence context; it will never be propagated.
- When a JTA application-managed EntityManager is used, if the EntityManager is created outside the scope of the current JTA transaction, it is the responsiblity of the application to associate the EntityManager with the transaction by calling entityManager.joinTransaction().

USERTRANSACTION
- Only to be used in session beans (or MDB's) with BMT.
- When to use EntityTransaction of UserTransaction? When a bean is CMT, we should use EntityTransaction and when it's BMT, we should use UserTransaction. Is this statement correct??


SCJP 1.5, SCWCD 1.4, SCBCD 5
roldino Harris
Greenhorn

Joined: Jul 17, 2008
Posts: 5
I think most of the assertments are correct. Except this :

APPLICATION-MANAGED RESOURCE-LOCAL ENTITYMANAGER
- Uses EntityTransaction for transaction management
- Created with @PersistenceUnit


should be

APPLICATION-MANAGED RESOURCE-LOCAL ENTITYMANAGER
- Uses EntityTransaction for transaction management
- Created with EntityManagerFactory

and to this

USERTRANSACTION
- Only to be used in session beans (or MDB's) with BMT.
- When to use EntityTransaction of UserTransaction? When a bean is CMT, we should use EntityTransaction and when it's BMT, we should use UserTransaction. Is this statement correct??


Here is what I think:

We have two types of application-managed Entity Manager (EntityManager created with EntityManagerFactory):
- one with EntityTransaction. we use this type when there is no JTA (No Dependency injection allow)
- one with UserTransaction when there is JTA allowed. In this case it is a BMT Bean


I hope this help







SCJP 5 (85%), SCWCD (95%), SCBCD (83%)
roldino Harris
Greenhorn

Joined: Jul 17, 2008
Posts: 5
Hey Tim,

I think most of what you wrote are correct. Except this :

APPLICATION-MANAGED RESOURCE-LOCAL ENTITYMANAGER
- Uses EntityTransaction for transaction management
- Created with @PersistenceUnit


should be

APPLICATION-MANAGED RESOURCE-LOCAL ENTITYMANAGER
- Uses EntityTransaction for transaction management
- Created with EntityManagerFactory

and to this

USERTRANSACTION
- Only to be used in session beans (or MDB's) with BMT.
- When to use EntityTransaction of UserTransaction? When a bean is CMT, we should use EntityTransaction and when it's BMT, we should use UserTransaction. Is this statement correct??


Here is what I think:

We have two types of application-managed Entity Manager (EntityManager created with EntityManagerFactory):
- one with EntityTransaction. we use this type when there is no JTA (No Dependency injection allow)
- one with UserTransaction when there is JTA allowed. In this case it is a BMT Bean


I hope this help






Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

"kam",
Welcome to the ranch. You may not be aware of the ranch Naming Policy. Please read it carefully and change your name accordingly (you need to set both your first and last name). Thank you.


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EntityManagers and transactions
 
Similar Threads
EJB3 Beta Certification - EntityManager BASIC notes.
EJB3 Beta Certification - Help =))
Passed SCBCD 5.0 :)
Persitence confusion. Please help.
Transaction Scoped Persistence Contexts