Search |
||
A simple method call: that is all it takesPosted by mister__m on December 29, 2003 at 6:56 PM PST
Not having the burden of managing transactions by yourself - a.k.a Container Managed Transactions, CMT for short - is a compelling reason for using EJBs. Obviously, EJB is not the only technology that gives you that, but that's a entirely different discussion. Back to the point, the fact you don't have to call any transaction management method neither in The most obvious thing - at least it should be - you should do is to specify which transaction attribute applies to each method in each EJB you wrote. There are a bunch of ways of doing it in the deployment descriptor, from specifying different attributes to each overloaded version of a method to using the same for all of them, and some containers have decent default values for when you don't declare anything, but I'm not going to cover this here. Today's point is more subtle than that and is something I've seen a considerable number of developers - including some good ones - not doing: calling
Mark the current transaction for rollback. The transaction will become permanently marked for rollback. A transaction marked for rollback can never commit. Only enterprise beans with container-managed transactions are allowed to use this method. Note that only EJBs that use CMT - most of them - are allowed to use this method. But when are they required to? When it must be called? The answer is relatively simple, but astonishing for some people (from now on, everything here refers to CMT EJBs). Let's start to answer these questions by taking a look at section 17.3.4.2 of the recently published EJB 2.1 spec: Typically, an enterprise bean marks a transaction for rollback to protect data integrity before throwing an application exception, because application exceptions do not automatically cause the container to rollback the transaction. I highlighted the bold part and I wish it was already printed in bold in the pdf, actually. A lot of people read this section and simply ignore this important sentence. An application exception does not cause the container to rollback a transaction. It is a simple, but ignored fact. But what is an application exception? Section 18.1.1 defines it: An application exception is an exception defined in the throws clause of a method of an enterprise bean’s home, component, message listener, or web service endpoint interface, other than the Enterprise bean business methods use application exceptions to inform the client of abnormal application-level conditions, such as unacceptable values of the input arguments to a business method. A client can typically recover from an application exception. Application exceptions are not intended for reporting system-level problems. Although the explanation is somewhat deceiving - Java won't stop you from declaring any The Bean Provider can rely on the container to perform the following tasks when catching a non-application exception:
So, if you throw a
// Some code goes here
public BigDecimal withdraw(BigDecimal amount) throws InsufficientBalanceException {
BigDecimal balance = getBalance();
//Ooops, trying to do something that shouldn't be done
if (balance.compareTo(amount) < 0) {
// This call is necessary as the exception to be thrown does not derive from RuntimeException
entityContext.setRollbackOnly();
throw new InsufficientBalanceException(balance, amount);
}
balance = balance.subtract(amount);
setBalance(balance);
return balance;
}
// And more code goes here
Throwing a checked exception is not enough; you need to call To end up, be aware that The container must throw the So, if all you are bean methods are configured with the »
Related Topics >>
J2EE Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|