Win a copy of Microservices Testing (Live Project) this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Java Beat Question - persi. cntxt

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
6) Which of the following statements are true about Persistence Contexts?

a. The entities managed by Extended Persistence Context won't go to the database unless they are associated with some active transaction in a J2SE environment.
b. There is no need to associate Extended Persistence Context to an active transaction.
c. In a transactional-scoped persistence context, the entities will become detached as soon as the transaction ends.
d. All the above.

The correct options are c and a. Option c is obvious. But I did not get option a. The answer explanation says -

Unless an extended persistence context is manually enlisted in a transaction, the entities won't go into the database.

does this mean something like the pseudo code below?

//start transation
//entity operations
//end transaction

Also, I am bit unsure how one can create transaction and extended persistence contexts with application managed entity managers in a java se environment. some code snippets would be great to see!
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

extended persistence contexts with application managed entity managers in a java se environment. some code snippets would be great to see!



code image
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

does this mean something like the pseudo code below?


Yes, almost:

//start transation
entityManager.joinTransaction()
//entity operations
//end transaction

would do. In BMT you have to code the

//start transaction
// end transaction

parts, while in CMT the container handles these parts automatically.

Please note that there are two types of extended persistence contexts:

- persistence context of an application managed entity manager
- persistence context of an container managed entity manager of a stateful session bean with persistence type EXTENDED

Answer a) holds for both, but the explanation

Unless an extended persistence context is manually enlisted in a transaction, the entities won't go into the database.


only holds for application managed entity managers: An extended persistence context of an container managed entity manager will be automatically associated with an active transaction.

And there is another exception to the quoted explanation: If an application managed entity manager is created within an transaction, its persistence context is automatically associated with the transaction. No joinTransaction() is needed in this situation. But answer a) is still right.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, when it comes to transactions-JavaSE following things hold-

1. RESOURCE_LOCAL in persistence.xml
2. entity managers are application managed, with extended persistence context (always).
3. joinTransaction( ) not needed as since the EntityManager itself is creating the transaction instead of the container,
it implicitly keeps track of EntityTransactions, so the join is not necessary.

4. need to explicitly close the entity manager and entity manager factory.

Can any one help me list such points for transactions-JavaEE? Please correct if I missed something in the above list!
 
reply
    Bookmark Topic Watch Topic
  • New Topic