phewataal
phewataal

Reputation: 1117

Handling TransactionTimeout Exception in EJB

I have an EJB method called methodA() calling another EJB method called methodB() which starts a new container managed transaction. in methodB, I am forcing a transaction timeout which is rightly caught by caughtB and not propagated to methodA. But I am surprised that methodA receives the Exception. Am I missing soemthing here?

methodA() {
 try {
   methodB();
   System.out.println("print me!");
 } catch(Exception e) {
   System.out.println("shouldn't be here");
 }
}

@TransactionTimeout(5) //5 sec timeout
methodB() {
  try {
    Thread.sleep(6000);
  } catch(Throwable t) {
    System.out.println("Eating all the Exception..");
  }
}

The first method should have never caught the exception (EJBTransactionTimeoutException) because the methodB have eaten it. I am seeing the output of "Shouldn't be here" instead of "print me!". It makes me wonder if container throws yet another exception of EJBTransactionTimeoutException immediately after methodB is completed although it has already thrown Timeout exception?

Upvotes: 1

Views: 5579

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

Thread.sleep() never throws a TransactionTimeoutException, so there is no way this catch block catches this exception.

When EJB A calls methodB from EJB B, it doesn't call the bean method directly. It calls methodB on a proxy. This proxy is responsible for handling the transaction management, the security, etc. and then call the actual methodB on your bean instance.

So the exception is not thrown from withing the methodB. It's called from the proxy which wraps the EJB B, and which throws a TransactionTimeoutException when methodB of the wrapped bean instance returns, and has taken more than the configured timeout to execute.

Upvotes: 7

Related Questions