Section 7.2. Configuring Simple Transactions

team bbl


7.2. Configuring Simple Transactions

Now, you'll see the most useful type of transaction support: declarative transactions. It's time to go back and explain the configuration of the transactions on your façade. In Chapter 4, you configured the transactions without explanation. It's time to go back and fill in the holes.

Declarative transactions are perhaps the most important feature of EJB session beans. Many developers decide not to use EJB at all when they discover that you can in fact use declarative transactions without sacrificing your first-born child.

You will implement declarative transactions without ever touching the code. You only need to change the context. Just like the examples in the previous chapter, you will configure an interceptor. The interceptor defines the methods to be marked as transactional and defines their expected behavior. Some of the methods will use full transaction propagation, and some will use a lighter-weight, read-only propagation (Example 7-3).

Example 7-3. RentABike-servlet.xml
<bean name="transactionInterceptor" >         <property name="transactionManager">             <ref local="transactionManager"/>         </property>         <property name="transactionAttributeSource">             <value>                             com.springbook.RentABike.transferReservation=             PROPAGATION_REQUIRED,-ReservationTransferException             com.springbook.RentABike.save*=PROPAGATION_REQUIRED             com.springbook.RentABike.*=PROPAGATION_REQUIRED,readOnly             </value>         </property>     </bean>

In the definition of the behavior for the transferReservation method, notice that you pass the required transaction flag and the name of a checked exception. This tells your interceptor to roll back the transaction if an exception of that type is thrown; otherwise, not.

Next, you'll build a proxy that specifies the target bean, like in Example 7-4.

Example 7-4. RentABike-servlet.xml
<bean  >        <property name="proxyInterfaces">         <value>com.springbook.RentABike</value>        </property>        <property name="interceptorNames">           <value>transactionInterceptor,rentaBikeTarget</value>       </property> </bean>

The execution remains unchanged.


Note: Think of this bean as the behavior in the original target, plus all of the aspects that you've added. In this case, the rentaBike is a transactional version of the target.

7.2.1. What just happened?

You just configured declarative transactions on a POJO. When any of the methods that we specified starts, the transaction advisor will tell the underlying transaction strategy to start the transaction. When the method successfully completes, the advisor will commit the transaction. If there's an exception, the advisor will roll back. That's exactly the behavior that you're looking for.

7.2.2. What about...

Java Transaction API, or JTA? So far, you've used lightweight transactions. The nice thing about Spring is that you can change the transaction strategy without rewriting any code. Spring fully supports JTA, so you can use it to span multiple databases or perform distributed transactions (called XA transactions).

XA transactions may be more powerful than the alternatives, but you don't always need them. In fact, there are at least two good reasons to use something else:

  • You may need individual features in a more direct transactional API, such as the isolation level support from JDBC transactions. In that case, XA transactions won't work, because they don't provide domain-specific abstractions.

  • You might need better performance. If XA transactions are overkill (such as applications that only access a single database), then XA transactions are likely to be much slower than an alternative, such as HibernateTransactions.

JTA tends to be the best fit when you're using multiple resources. If your application needs to commit data from the same transaction across multiple databases, then JTA will likely be your best fit.

    team bbl



    Spring. A developer's Notebook
    Spring: A Developers Notebook
    ISBN: 0596009100
    EAN: 2147483647
    Year: 2005
    Pages: 90

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net