File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes question about application-managed Entity manager 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 "question about application-managed Entity manager" Watch "question about application-managed Entity manager" New topic
Author

question about application-managed Entity manager

vitesse wei
Ranch Hand

Joined: Sep 07, 2007
Posts: 100
question:

Given this Java EE application that uses a JTA application-managed entity manager:
20. UserTransaction utx = ...;
21. utx.begin();
22. //insert code here
23. utx.commit();

Which two code fragments can be used on Line 22 to persist an order instance assuming that all
references are property initialized? (Choose two.)

A. em.merge(order);
em.flush();
B. em.persist(order);
em.flush();
C. em.joinTransaction();
em.persist(order);
D. em = emf.createEntityManager();
em.persist(order);

Given Answer is C,D,anyone please explain why A and B are wrong?


SCJP 5.0<br />SCWCD1.4<br />SCBCD5
Tushar Roy
Greenhorn

Joined: Mar 26, 2008
Posts: 29
Application Managed Entity Manager created outside the transaction do not automatically join the transaction. So case A and B will give exception. In C the entity manager joins the transaction using joinTransaction method and in D the entity manager is created in the transaction. So they are correct answers.


Tushar<br /> <br />SCJP 5 (95%)<br />SCWCD 1.4 (92%)<br />SCBCD 5 (93%)
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Hi,
I want to bring back this question again.
Why B is wrong here.
Do I really need to join the transaction in application managed entitymanager case?And Why D is correct?


Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
According to spec:

1). EntityManager.flush method must be called within an active transaction context.
2). It is the responsibility of the application to associate the entity manager with the transaction by calling EntityManager.joinTransaction.

Therefore calling flush, without associating the application-managed entity manger (that was created outside the scope of the active transaction) with the active JTA transaction, results in throwing TransactionRequiredException. According to spec, A and B are wrong.

However, I tried it in Glassfish and it was not required to explicitly call EntityManager.joinTransaction, I was able to sucessfully perform operations from A and B without calling EntityManager.joinTransaction. Glassfish uses Toplink which provides the JPA functionality for the EJB 3.0 Reference Implementation. It is probably another Glassfish's inconsistency with spec.
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
so, you mean so say the following code is wrong:

If I call the above code from a tractional code, would I get exception saying you need to join to the current transaction?
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
In this case exception will not be thrown. The persist, merge, remove, and refresh methods MUST be invoked within a transaction context when an entity manager with a transaction-scoped persistence context is used. We use application-managed persistence context which by default is EXTENDED, so participating in the active transaction is not needed. However, since entity manager does not participate in transaction and EntityManager.flush method is not invoked 'emp' object state will not be saved to the database.
RakeshS Chauhan
Greenhorn

Joined: Apr 05, 2008
Posts: 4
hi krishna,
here are the following lines from "EJB 3 Java Persistence API" book.
-------
An application-managed entity manager participates in a JTA transaction in one of two
ways. If the persistence context is created inside the transaction, then the persistence provider
will automatically synchronize the persistence context with the transaction. If the persistence
context was created earlier (outside of a transaction or in a transaction that has since ended),
the persistence context may be manually synchronized with the transaction by calling
joinTransaction() on the EntityManager interface. Once synchronized, the persistence context
will automatically be flushed when the transaction commits.
--------------
This will make the answer more clear.
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Hi Please look at the following code:

I could able to successfully persist the manager object.
I am not using joinTrasaction() method.
And another finding is that, If use the following code after the catch block it is giving illegal state exception

Can anyone explain why this exception?
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
You are using container-managed entity manager and container is associating entity manager with the transaction. As for IllegalStateException, you are not allowed to invoke close on container-managed entity manager.
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Thank you very much.
Now I understood my mistake.
You know, Its very cumbersome to understand the EntityManager and Trasaction part in Ejb 3.0.
 
GeeCON Prague 2014
 
subject: question about application-managed Entity manager