Transaction support and the Spring Framework

For Spring Framework Version 2.5 or later, you 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, you can use a Spring Framework configuration that supports a restricted set of transaction attributes.

Declarative transaction model

WebSphere® Application Server Version 6.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:
  • PROPAGATION_REQUIRED
  • PROPAGATION_SUPPORTS
  • PROPAGATION_MANDATORY
  • PROPAGATION_REQUIRES_NEW
  • PROPAGATION_NOT_SUPPORTED
  • PROPAGATION_NEVER
Use the following declaration for the WebSphere Application Server 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

You 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:
  • All methods that start with the string get have the transaction attribute PROPAGATION_REQUIRED.
  • All methods that start with the string set have the transaction attribute PROPAGATION_REQUIRES_NEW.
  • All other methods use the default transaction settings.
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 you can apply the settings to the required operation by declaring a pointcut. You can apply the settings to various parts of the application. The following example code applies the settings to any operation that is 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, you need Java Platform, Standard Edition 5 (Java SE 5) or later. Therefore, you can use this method with WebSphere Application Server Version 6.1 or later.

Add the following line to the Spring.xml configuration:
<tx:annotation-driven/>
Mark any methods that require transactional attributes with the @Transactional annotation, for example:
@Transactional(readOnly = true)
public String getUserName()
{ ...
}

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

Transaction support with Spring Framework before Version 2.5

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

You can use this method of transaction support with versions of the Spring Framework before Version 2.5 that do not provide the WebSphereUowTransactionManager class. You can also use this method of transaction support with versions of WebSphere Application Server 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:
  • PROPAGATION_REQUIRED
  • PROPAGATION_SUPPORTS
  • PROPAGATION_MANDATORY
  • PROPAGATION_NEVER
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:
  • PROPAGATION_REQUIRES_NEW
  • PROPAGATION_NOT_SUPPORTED

WebSphere Application Server does not support the use of the Spring Framework class org.springframework.transaction.jta.WebSphereTransactionManagerFactoryBean.