Search This Blog

Monday 1 October 2012

Managing Transactions the Spring way 2

In the previous post we saw how to use TransactionProxyFactoryBean. For every class that needs transactional behavior, we need to add one such bean that will act as the proxy. For a big application this is a lot of xml definition. The Spring In Action book shows how this code can be reduced  by using abstract beans.
Most of the classes we work with have some add methods that need a transactional functionality. They may also have get methods that involve read operations to load data from the database.
<bean id="defaultTransactionProxyClient"
    <property name="transactionManager" ref="txManager" />
    <property name="transactionAttributes">
            <prop key="add*">PROPAGATION_REQUIRED</prop>
            <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
The first step is to define an abstract bean. This includes the common properties, such as a reference to the transaction manger. It also includes a default value for transactionAttributes. So the definition for a bean that does not have any additional or different special behavior:
<bean id="hibProxyClient"
    <property name="target" ref="hibProxyClientTarget" />
    <property name="proxyInterfaces" value="com.test.IProxyClient"/>
The bean is now much more simplified. It has the target attribute to indicate which bean is to be proxied.  
If the class implements an interface then the proxyInterfaces attribute is used for the same. The rest of the properties are inherited by the bean from its parent bean.

No comments:

Post a Comment