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.
But when I tried this example, logTransaction2 is running in a different transaction than createEmployee2 , but it still works. I am not sure why.
The quote says the second stateful bean with extended pc running in a different transaction will get an exception.
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.
7.6.3.1 Requirements for Persistence Context Propagation
...
If a component is called and the JTA transaction is propagated into that component:
If the component is a stateful session bean to which an extended persistence context has been bound and there is a different persistence context bound to the JTA transaction, an EJBException is thrown by the container.
Employee service: add employee 100
2017-03-22T21:14:08.863-0400|Info: Looked up audit service...
2017-03-22T21:14:08.863-0400|Info: Audit service: add employee 200
2017-03-22T21:14:13.071-0400|Info: Employee service: add employee 100
2017-03-22T21:14:13.071-0400|Info: Employee service bean asks audit service bean to find employee 300 created in m3:
2017-03-22T21:14:13.080-0400|Warning: A system exception occurred during an invocation on EJB AuditService, method: public void examples.stateless.AuditService.method4(int)
2017-03-22T21:14:13.080-0400|Warning: javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
at com.sun.ejb.containers.EJBContainerTransactionManager.checkExceptionClientTx(EJBContainerTransactionManager.java:662)
...
at examples.stateless.__EJB31_Generated__AuditService__Intf____Bean__.method4(Unknown Source)
at examples.stateless.EmployeeService.method3(EmployeeService.java:55)
...
Caused by: java.lang.IllegalArgumentException: Unknown employee id in method 4.
at examples.stateless.AuditService.method4(AuditService.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Employee service: add employee 100
2017-03-22T21:31:12.636-0400|Info: Looked up audit service...
2017-03-22T21:31:12.638-0400|Info: Audit service: add employee 200
2017-03-22T21:31:16.864-0400|Info: Employee service: add employee 100
2017-03-22T21:31:16.864-0400|Info: Employee service bean asks audit service bean to find employee 300 created in m3:
2017-03-22T21:31:16.880-0400|Info: Audit service also found employee id 300
2017-03-22T21:31:16.880-0400|Info: Employee service bean asks audit service bean to find employee 100 created in m1:
2017-03-22T21:31:16.880-0400|Info: Audit service also found employee id 100
2017-03-22T21:31:16.880-0400|Info: Employee service bean asks audit service bean to find employee 200 created in m2:
2017-03-22T21:31:16.880-0400|Info: Audit service also found employee id 200
Employee service: add employee 100
2017-03-22T21:36:52.546-0400|Info: Looked up audit service...
2017-03-22T21:36:52.546-0400|Info: Audit service: add employee 200
2017-03-22T21:36:56.637-0400|Info: Employee service: add employee 100
2017-03-22T21:36:56.637-0400|Info: Employee service bean asks audit service bean to find employee 300 created in m3:
2017-03-22T21:36:56.637-0400|Info: Audit service also found employee id 300
2017-03-22T21:36:56.637-0400|Info: Employee service bean asks audit service bean to find employee 100 created in m1:
2017-03-22T21:36:56.637-0400|Info: Audit service also found employee id 100
2017-03-22T21:36:56.637-0400|Info: Employee service bean asks audit service bean to find employee 200 created in m2:
2017-03-22T21:36:56.637-0400|Info: Audit service also found employee id 200