Enterprise Java Development@TOPIC@

Chapter 99. Persistence Context Propagation

99.1. Stateless Persistence Context Interaction
99.2. Stateful Facade Persistence Context Interaction
99.3. Transaction Rollbacks
99.3.1. Stateless Transaction Rollback
99.3.2. Stateful Transaction Rollback
99.4. Pessamistic Locking
99.5. Summary

Figure 99.1. Persistence and Transaction Context Propagation

Persistence and Transaction Context Propagation

  • EJB will instantiate persistence context if does not yet exist

  • Stateless EJBs may only have transaction-scope persistence contexts

  • Stateful EJBs may have transaction-scope or extended persistence contexts

  • EJBs can share persistence contexts

    • Stateless EJB can propagate its tx-scope persistence context to a called EJB

    • Stateful EJB can propagate its tx-scoped or extended persistence context to a called EJB

    • Stateless EJB can work with extended persistence context provided by upstream Stateful client

    • Stateful EJB cannot transform propagated tx-scope persistence context into an extended

  • EJB Facade can act as sharing point for common persistence context


  • (Error checking removed)

  • Locate specific room

  • Add guest to DB

  • Associate room with guest


  • Client performs actions one at a time


  • Persistence context created

  • Available rooms loaded into persistence context

  • Result is returned

  • Persistence context destroyed


  • Persistence context created

  • Specific room loaded into persistence context

  • Guest inserted into DB


  • Guest inserted into DB

Note

The transaction does not end until existing the Stateless EJB method. That means any DB constraint violations will not occur within the context of the EJB call that caused it unless you call em.flush() (as a form of debug) prior to exiting the business method.


  • Room.occupant foreign key updated

  • Guest returned to client

  • Persistence context destroyed


  • Stateful EJB injects @PersistenceContext -- propagated in downstream EJB calls

  • Extended persistence context -- may be associated with zero or more transactions

  • Performing in-memory actions outside of transaction boundary


  • Method executed within active JTA transaction with persistence context associated with transaction

  • Guests already managed but will be fully persisted in this method

  • Calls to Stateless HotelMgmtEJB executed on this EJB's persistence context

  • @Remove indicates stateful instance to be discarded after method called


  • Multiple requests are issued to Stateful EJB

  • Specific method(s) act on that state


  • em.persist() outside of transaction causing causing sequence call but no table inserts


  • Downstream Stateless HotelMgmtEJB queried for rooms

  • Rooms loaded into persistence context


  • SQL inserts issued to DB during flush (requires transaction active)


  • Stateless HotelMgmtEJB accesses Rooms from EntityManager first-level cache -- no additional DB access

  • Stateless HotelMgmtEJB updates Room.occupant FK to reference Guest -- who is also already managed and in first-level cache

  • JTA Transaction committed by container after Stateful EJB method exits

  • Stateful EJB and persistence context also destroyed after method exits


  • Rejects checkin when invalid

  • Completes check-in when valid


  • Stateless EJB commits each of these check-ins


  • Additional check-in rejected -- nothing committed


  • Each check-in occured in own transaction

  • Later error did not impact previous completed transactions


  • Same stateful process as before -- but with one additional Guest (one too many)


  • Client attempts to check-in to all Rooms

  • Checked exception will be thrown


  • Five (5) Guests are flushed to database prior to the rollback


  • Persisted Guests removed from database as a part of transaction rollback

  • Early check-ins never flushed to DB -- discarded as part of rollback


  • All check-ins associated with Rooms removed as a part of rollback


  • setLockMode(LockModeType.PESSIMISTIC_WRITE) - locks row (or table) for remainder of transaction

  • select ... FOR UPDATE issued for query