Question

I have read in the book that:

Typically, the container begins a transaction immediately before an enterprise bean method starts. It commits the transaction just before the method exits.

Suppose my stateful EJB has exposed 3 business methods and the client calls these 3 methods sequentially and want wants to run these 3 methods within a single transaction. These methods would be called across multiple requests.

Now the spec says that the transaction is committed just before the method exits. How will I be able to rollback the whole transaction, if my 1st method is successful and 2nd (or 3rd) method fails?

Please note that I don't want to use create my own transaction boundary.

Any help or the pointer in the right direction would be highly appreciated.

Was it helpful?

Solution

You are using a stateful session bean to act as a buffer. And you want a transaction around multiple calls.

From a design perspective, it's OK, if the transaction is started and committed/rollbacked from a single request (from within a single HttpServletRequest for example). In this case you can use a UserTransaction to span the transaction over multiple calls to an EJB. But a UserTransaction is bound to the current thread, so it might be difficult to pass it to the next request.

If you start and commit from different requests, you lose control over the duration of the transaction: Now a client controls the transaction. If that client crashes, the transaction won't be rolled back until the transaction timeout is reached. So the recommendation in this case is to buffer in a HttpSession for example. If all data has been collected, call a (stateless) EJB to persist it.

OTHER TIPS

Create a method in the bean that calls all the other 3 methods. Then they'll be in the same transaction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top