Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

EntityManagers and transactions

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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






 
roldino Harris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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






 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic