The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
March 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Removing an EJB in a transaction

Posted by Frank LaRosa on March 02, 2001 at 10:29 PM

Consider the following problem:

(1) I have two stateful session EJBs, A and B, each with one method. The trans-attribute for both methods is "Required". Both EJBs also implement the SessionSynchronization interface.

(2) The method in EJB A is called from a servlet. The container starts a transaction.

(3) The method in EJB A instantiates a new EJB B and calls the method in it. The container passes along the transaction context from EJB A.

(4) Now the "A" method is done with the "B" EJB, and wants to remove it. But, I can't remove the EJB at this point, because it's still participating in the transaction -- removing an EJB while it's in a transaction fails with a RemoteException.

(5) To solve the problem, I tried making the reference to EJB B an instance variable of EJB A and removing it in the afterCompletion() method of EJB A. This works -- but only about half the time.

(6) I put logging messages in the afterCompletion() methods for both EJBs. It turns out that the order in which the container calls afterCompletion() is random. Sometimes it calls EJB B's
afterCompletion() method first, and in those cases, EJB A is able to remove EJB B in its afterCompletion() method. In the other case, it is not -- same RemoteException as when I tried to remove it in the method call.

So, here are my questions:

- Is there a better way to do this? How?

- Do I need to worry about removing EJB B at all, or should I just let the container remove it after it times out? The EJB *does* allocate a resource (a JMS connection) in its ejbCreate() function, and closes it in ejbRemove(). So I really hate to just leave it there with an open connection until the container decides to clean it up.

Thanks!



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us