• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

question about application-managed Entity manager

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic