• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

About persistence context inheritance and transaction context

 
Bartender
Posts: 2447
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to this document :https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/4.2/html/Hibernate_EntityManager_User_Guide/sect-Hibernate_EntityManager_User_Guide-EJB_container_environment-Persistence_context_propagation.html


If a stateful session bean with an extended persistence context instantiates another stateful session bean with an extended persistence context, the extended persistence context is inherited by the second stateful session bean. If the second stateful session bean is called with a different transaction context than the first, an IllegalStateException is thrown



I ran the chapter 6 example , persistenceContextInheritance:



Based on the quote, it should not work because the init() of EmployeeManager is in a new transaction context due to the REQUIRES_NEW.
But it works.
 
Himai Minh
Bartender
Posts: 2447
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, the 7.6.3.1 Requirements for pc propagation does not say too much detail about when a pc should be propagation or should not when a pc is inherited from the calling bean.

Let's say stateful Bean A has method1.
stateful Bean B (inherits A's pc)  has method2.
Bean A injects Bean A and  method1 calls method2.
What if  method2's transaction is a new transaction ?
The JSR 317 does not say if the the inherited PC  should be run in the parent's PC; otherwise an exception must be thrown.
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What if  method2's transaction is a new transaction ?  


From the same spec section, let me try to write it a little bit different:

If a component is called and there is
  • no JTA transaction or
  • the JTA transaction is not propagated,
  • then the persistence context is not propagated.

    So in your example "What if  method2's transaction is a new transaction ?" the JTA transaction is not propagated (REQUIRES_NEW will suspend the active JTA transaction and the container will start a new JTA transaction) which means that the persistence context is also not propagated.
     
    Himai Minh
    Bartender
    Posts: 2447
    13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi, I tried to modify the chapter 6's persistenceContextInheritance example,




    The employee manager starts a new transaction, but it still inherits the persistence context from department manager even though employee manager uses a new JTA transaction.
    It prints "Employee 99 is found."
    I guess the employee 99 has already persisted in the DB before the department manager finishes the transaction it init() method.
     
    Himai Minh
    Bartender
    Posts: 2447
    13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This time, I use BMT instead of CMT.
    Assume there is no employee 99 in the DB




    The department manager persists employee 99, but has not committed yet. Then, it calls the employee manager, which can find employee 99 from the entity manager.
    The DB still does not have employee 99 in the table.
    To my surprise, employee manager uses a different JTA transaction. But it can inherit the persistence context from the JTA transaction of department manager.
     
    Himai Minh
    Bartender
    Posts: 2447
    13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Another example for reference and comparison.
    I use stateless bean with persistence type=transaction. Obviously, the persistence context is not propagated from one stateless bean to another.




    Output: employee 99 is not found.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3412
    320
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    To my surprise, employee manager uses a different JTA transaction. But it can inherit the persistence context from the JTA transaction of department manager.


    hmmm, interesting but I see what is happening: there is an exception for Stateful Session beans:


    7.6.2.1 Inheritance of Extended Persistence Context
    If a stateful session bean instantiates a stateful session bean (executing in the same EJB container instance) which also has such an extended persistence context, the extended persistence context of the first stateful session bean is inherited by the second stateful session bean and bound to it, and this rule recursively applies—independently of whether transactions are active or not at the point of the creation of the stateful session beans.

     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic