• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Entity Managers - Types

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of these is true?

1.Container Managed Entity Managers must use resource-local transactions.
2.Container Managed Entity Managers must use JTA transactions.
3.Application Managed Entity Managers must use JTA transactions.
4.Application Managed Entity Managers must use resource-local transactions.

..and why..
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Container managed entity managers are always JTA. Application managed entity managers can be either resource-local or JTA (this can be specified in the persistence.xml).

When we cannot use JTA UserTransaction (example: J2SE Swing application), we can use resource-local (EntityTransaction) as we wished, that is why it is available in application managed entity managers.

Container managed entity managers are always managed by the J2EE container, where JTA is available. No need to use resource-local transactions here. That is why it is not available in container managed entity managers.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aggreed, with one comment - you can actually use JTA with application managed, when you have a JTA implementation available, like JOTM, Atomicos for example.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clark,

Thanks for the input. So option 2. is correct. To conclude -

Container managed EM - only Java EE - JTA Only - container does it for you
Application Managed EM - only Java EE/SE - JTA/resource-local - you code for transaction management

So the above translates into the following possible cases :
Application Managed transaction management, when done in Java EE, and we want to use JTA
UserTransaction ut = ctx.getUserTransaction(); where ctx = SessionContext if it's a SB else MessageDrivenContext if it's a MDB

Application managed transaction management when done in Java SE and we use resource-local (the only available option)
EntityTransaction et = em.getTransaction(); where em = EntityManager

Application Managed transaction management, when done in Java EE, and we want to use resource-local
EntityTransaction et = em.getTransaction(); where em = EntityManager


Am I correct? I have added the bold part after Clark validated my earlier post (without the bold [art)

Raf,
Thanks for the value addition.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niranjan Deshpande wrote:Clark,

Thanks for the input. So option 2. is correct. To conclude -

Container managed EM - only Java EE - JTA Only
Application Managed EM - only Java EE/SE - JTA/resource-local.

Am I correct?


Yes you are correct "Deshpande".
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am afraid I would mis-spell your first name. CLARK is a simpler option..

...good to see some humour... in the tensed up preparation

..thanks again..

Can anyone please validate the part in bold, that i newly added? I think I am right! feeling confident .. but please validate..
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please validate?
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niranjan,
Application Managed transaction management, when done in Java EE, and we want to use JTA
UserTransaction ut = ctx.getUserTransaction(); where ctx = SessionContext if it's a SB else MessageDrivenContext if it's a MDB
you don't have to use BMT when using an application managed JTA entity manager. You can use CMT, too.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ralph,

Could you be kind to list out the code for the thing that you are mentioning? Does that mean I have a 5th possible case to add in the code above?
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean I have a 5th possible case to add in the code above?

I see just 3 code fragments above, which is the 4th one ?

No, you don't have an additional item in your list. I just wanted to point out that a JTA transaction can be bean-managed (as in your sample code) as well as container-managed.

Code sample: Just take any CMT bean with an application managed entity manager.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yups, I messed up the count in the code list its just 3.

Code sample: Just take any CMT bean with an application managed entity manager.


You mean, a @TransactionManagement(TransactionManagementType.CONTAINER) on the bean, and a EJBContext injected into the bean. Right.
This will be added as point in code 1 of my list?

By the way, talking in terms of low level system details, what's the difference in JTA and resource-local apart from the environment that they are applied in?
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I think, the subject of this post should be CMT and BMT instead of Entity Managers - types.

We are discussing about 'transactions' and not entity managers.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic