Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when stateful bean is destroyed during @Asynchronous method call?

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?

like image 835
andbi Avatar asked Nov 10 '22 14:11

andbi


1 Answers

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.

like image 141
Sergio Avatar answered Nov 15 '22 12:11

Sergio