aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Entity Managers - Types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Entity Managers - Types" Watch "Entity Managers - Types" New topic
Author

Entity Managers - Types

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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..

SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
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

Joined: Aug 21, 2008
Posts: 383
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

Joined: Oct 16, 2005
Posts: 1277
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

Joined: Nov 12, 2008
Posts: 757
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

Joined: Oct 16, 2005
Posts: 1277


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

Joined: Oct 16, 2005
Posts: 1277
Can anyone please validate?
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
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.


SCJP 5 (98%) - SCBCD 5 (98%)
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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

Joined: Apr 27, 2008
Posts: 342
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

Joined: Oct 16, 2005
Posts: 1277
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

Joined: Oct 16, 2005
Posts: 1277
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Entity Managers - Types