Maybe I'm misunderstanding stateless session beans then. I was under the impression that a SLSB had these properties:
1. All calls are serialized to the SLSB by the container (thread safety).
2. All method invocations on the SLSB happen in the same thread.
3. If a transaction is present when a client invokes a method on a SLSB, and the method is properly marked, then the method invocation will participate in the client's transaction.
With these three things in mind, doesn't it make sense that method calls to a SLSB, while all occurring in the same thread, could be interleaved between multiple clients and transactions? For example, take this pseudo-code running on the client side:
Code:
UserTransaction tx = [get UserTransaction];
tx.begin();
MySLSB slsb = [get SLSB];
slsb.method1();
slsb.method2();
tx.commit();
Imagine two clients executing this at the exact same time. The first client is "Client A", and Client A's transaction we'll call "Transaction A"... likewise for "Client B". The server may choose to serialize calls to a single instance of the stateless session bean, and it may do it in this order:
1. Client A calls method1() which participates in Transaction A
2. Client B calls method1() participating in Transaction B
3. Client A calls method2() participating in Transaction A
4. Client B calls method2() participating in Transaction B
Here we have four method invocations from two clients in two separate transactions being serialized to the same instance on the same thread. If we want to keep a Session for the duration of a transaction (on the server side), and if I understand all this correctly, then ThreadLocal alone won't work.. we'll need a way to associate a Session with a transaction. When a method is called, on the server side, we pull the Session for the current transaction.
Does this make sense, or am I mistaken?
Thanks,
Anodos