I have a stateful EJB with a transactional @Asynchronous
method returning Future<T>
. It's being called from web-tier (@SessionScoped
CDI bean) as shown below:
@SessionScoped
@Named
public class SessionBean {
@EJB
EjbService service
public void call() {
Future<Object> response = service.process();
}
}
@Stateful
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EjbService {
@Asynchronous
public Future<Object> process() {
//
}
}
The question is what happens to the transaction if a user terminates the web session during the execution of this asynchronous call?
The @Asynchronous
method creates a new transaction (even if the class has another attribute, the TransactionAttributeType.REQUIRED
will create a new transaction anyway).
Now, if you call get()
method in Future<V>
the thread will wait until the AsyncResult becomes available at the end of computation. After that, the Stateful could execute the timeout and will be destroyed by the container.
If you just execute the Async method -without get()
-, the method will be queued and processed, then, the same applies... Stateful destroyed.
pro tip: the ejb container maintains the result value for complete asynchronous invocations for a certain time (this time is not defined in the spec).
All in all, because a new transaction occurs, and the control is returned to the client immediately (unless call get()
), the transaction finished without exceptions under a normal scenario.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With