Let's modify your example a bit
@Stateless
public class MyBean1 {
@Resource
private SessionContext sessionContext;
pulic void method1() {
// method implementation
// As a side-effect, something is written into a database
// using an XA data source,
// and a message is sent using XA JMS
// (both under control of an XA transaction)
}
pulic int method2(int i) {
return i * i;
}
}
For example, the session context is used to get the UserTransaction
and getCallerPrincipal
. They are not necessarily always the same (when two clients all the EJB). As for the UserTransaction
: This one is bound to the current thread (see Javadoc).
As the session context is stored in a field (and not passed an argument to each individual method), the same EJB instance cannot be accessed by two different clients.
Therefore the specification requires the container to serialize calls to the same instance.
If you look at method2
, a purely functional implementation without any side-effect, there is no need for EJBs.