Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default EJB transaction mode for asynchronous methods?

  1. When I have an @Asynchronous method in an EJB, and I don't specify the @TransactionAttribute, then how exactly does the container handle the transaction boundaries? Obviously, it can't use the calling thread's transaction, so what does it do?

  2. Same question, but regarding methods that are triggered by the TimerService.


EDIT: I think I phrased that poorly. I already know that the default mode is 'REQUIRED'. So it should be safe to assume that those methods will always be called within a transaction. But my question is, what does that transaction's life-cycle look like? Does the container create a new transaction for each call? Or does it re-use the same transaction for all the calls on an asynchronous worker thread? If it's the latter, then when does the transaction get closed?

like image 832
Mike Baranczak Avatar asked Oct 25 '10 15:10

Mike Baranczak


People also ask

What is the default transaction attribute to a method in an EJB?

Yes, CONTAINER and REQUIRED are the default.

What is the default transaction attribute to a method in an EJB no explicit transaction attribute is declared in both class and method levels?

The Required attribute is the implicit transaction attribute for all enterprise bean methods running with container-managed transaction demarcation.

What is EJB transaction?

EJB Container/Servers are transaction servers and handles transactions context propagation and distributed transactions. Transactions can be managed by the container or by custom code handling in bean's code. Container Managed Transactions − In this type, the container manages the transaction states.

What are asynchronous business methods?

Asynchronous methods are typically used for long-running operations, for processor-intensive tasks, for background tasks, to increase application throughput, or to improve application response time if the method invocation result isn't required immediately.

How can I call an EJB method asynchronously?

Now with EJB 3.1, you can use a simple session bean with the @Asynchronous annotation on the method which must be called asynchronously. Two ways of asynchronous EJB invocations are used:

What are the attributes of transactions in EJB containers?

EJB 3.0 has specified following attributes of transactions, which EJB containers implement − REQUIRED − Indicates that business method has to be executed within transaction, otherwise a new transaction will be started for that method. REQUIRES_NEW − Indicates that a new transaction, is to be started for the business method.

What is Bean Managed Transactions in EJB?

Bean Managed Transactions − In this type, the developer manages the life cycle of transaction states. EJB 3.0 has specified following attributes of transactions, which EJB containers implement −

What is EJB transaction in Java?

EJB - Transactions. EJB Container/Servers are transaction servers and handles transactions context propagation and distributed transactions. Transactions can be managed by the container or by custom code handling in bean's code. Container Managed Transactions − In this type, the container manages the transaction states.


1 Answers

Similar to an MDB the transaction is started by the container just before your @Asynchronous, @Schedule or @Timeout method (and applicable interceptors) is actually invoked and committed just after the method (and interceptors) completes.

As per usual, the transaction propagates to all beans called in said method and all beans those beans call, recursively. Of course other beans invoked are welcome to change the transaction semantics of their method call via specifying other @TransactionAttribute settings (say REQUIRES_NEW, or NOT_SUPPORTED).

Side note, transactions are never propagated to beans with @TransactionManagement(BEAN). The container will always suspend any transaction in progress before invoking a method on a Bean-Managed Transaction bean.

EDIT:

Answering the edited question more directly; there are no means via the Java Transaction API to have one transaction span several threads and therefore no means in EJB. Every @Asynchronous call is completely disconnected from the caller's transaction (if any). You will essentially get either a new transaction, no transaction, or an exception depending on what @TransactionAttribute you used. Same with any calls from the TimerService.

like image 174
David Blevins Avatar answered Sep 19 '22 00:09

David Blevins