+

Search Tips   |   Advanced Search

Transaction support and the Spring Framework

For Spring Framework Version 2.5 or later, we can use the declarative transaction model, use the Spring Framework support for the AspectJ programming extension, or use annotation-based transaction support. For versions of the Spring Framework earlier than Version 2.5, and for versions of the application server that do not provide the UOWManager interface, we can use a Spring Framework configuration that supports a restricted set of transaction attributes.


Declarative transaction model

WAS v6.0.2.19 or later and Version 6.1.0.9 or later support the Spring Framework declarative transaction model to drive resource updates under transactional control. The WebSphereUowTransactionManager class in Spring Framework 2.5 uses the UOWManager interface in the application server to manage the transaction context. Because transaction demarcation is managed through the UOWManager interface, an appropriate global transaction or local transaction containment (LTC) context is always available when a resource provider is accessed. For more information about the UOWManager interface and Java Transaction API (JTA) support, see the related topic.

The WebSphereUowTransactionManager class supports the following Spring Framework transaction attributes:

Use the following declaration for the WAS transaction support:

<bean id="transactionManager"
   class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>

A Spring bean that references the previous declaration can then use Spring Framework dependency injection to use the transaction support. For example:

<bean id="someBean" class="some.class">
   <property name="transactionManager" >
      <ref bean="transactionManager"/>
   </property>
...
</bean>
<property name="transactionAttributes">
   <props>
      <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
</property>


The AspectJ programming extension

We can use the Spring Framework support for the AspectJ programming extension. The following example code declares a <tx:advice/> element with the following transactional behavior:

For example:

<tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
    <tx:method name="get*" propagation="REQUIRED" read-only="true" />
    <tx:method name="set*" propagation="REQUIRES_NEW" />
    <tx:method name="*" />
  </tx:attributes>
</tx:advice>

Then we can apply the settings to the required operation by declaring a pointcut. We can apply the settings to various parts of the application. The following example code applies the settings to any operation defined in the class MyService.

<aop:config>
  <aop:pointcut id="myServiceOperation"                        expression="execution(* sample.service.MyService.*(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="myServiceOperation"/>
</aop:config>


Annotation-based transaction support

To use the annotation-based transaction support, we need Java Platform, Standard Edition 5 (Java SE 5) or later. Therefore, we can use this method with WAS v6.1 or later.

Add the following line to the Spring.xml configuration:

Mark any methods that require transactional attributes with the @Transactional annotation, for example:

@Transactional(readOnly = true)
public String getUserName()
{ ...
}

We can use the @Transactional annotation to annotate only public methods.


Transaction support with Spring Framework before Version 2.5

We can use a Spring Framework configuration that supports a restricted set of transaction attributes.

We can use this method of transaction support with versions of the Spring Framework before Version 2.5 that do not provide the WebSphereUowTransactionManager class. We can also use this method of transaction support with versions of WAS earlier than Version 6.0.2.19 and Version 6.1.0.9 that do not provide the UOWManager interface.

The configuration supports the following Spring Framework transaction attributes:

Use the following Spring Framework configuration:

<bean id="transactionManager"                class="org.springframework.transaction.jta.JtaTransactionManager">
  <property name="autodetectTransactionManager"value="false" />
</bean>

The configuration does not support the following Spring Framework transaction attributes:

WAS does not support the use of the Spring Framework class org.springframework.transaction.jta.WebSphereTransactionManagerFactoryBean.


Related concepts

  • JTA support


    Related information:

    Spring Documentation