ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Bean-Managed Transaction Suspension in J2EE
Pages: 1, 2, 3, 4

TransactionManager Usage in the Spring Framework

The techniques described above are used extensively in the popular Spring framework for transactional proxies. You should be aware that every time you configure Spring's TransactionProxyFactoryBean with PROPAGATION_REQUIRES_NEW or PROPAGATION_NOT_SUPPORTED, transaction attributes, and JtaTransactionManager, Spring uses JTA's TransactionManager for transaction suspension and resumption. Spring is smart enough--sometimes too smart for my taste--to discover TransactionManager in your container, even if you don't specify it. For example, you can supply a JNDI name for UserTransaction when defining JtaTransactionManager in a Spring application context and it will "autodetect" TransactionManager if UserTransaction also implements it; as shown above, this is true for WebLogic, Orion, and Oracle OC4J.



Sometimes, this could be undesirable, especially if you want to stay strictly within a J2EE/EJB specification and ensure complete portability across all J2EE containers. Sometimes, as we've seen above, there could be issues with programmatic transaction suspend and resume, although Spring knows how to work around some of them, at least the issue described above with marking transaction for rollback before suspending in WebLogic. In this case, you can set the autodetectTransactionManager property to false when configuring JtaTransactionManager. If you do so, any attempt to use PROPAGATION_REQUIRES_NEW or PROPAGATION_NOT_SUPPORTED transactional attributes should fail with TransactionSuspensionNotSupportedException, but PROPAGATION_REQUIRED, PROPAGATION_SUPPORTS, PROPAGATION_MANDATORY, and PROPAGATION_NEVER should work normally. This corresponds to the functionality provided by the JTA UserTransaction and is guaranteed to work in any J2EE-compatible container.

Below is the transaction manager definition in a Spring application context with TransactionManager autodetect disabled.


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

Conclusion

Support for the JTA TransactionManager interface is not required by the J2EE specification, but since J2EE uses JTA as its underlying transaction infrastructure, almost all J2EE servers expose it as an extension to J2EE. There are some known issues with compatibility, and under certain circumstances, container-specific code can be used to work around it. Nevertheless, using TransactionManager in J2EE is a powerful feature if you need to implement programmatic transaction suspension. Just always test it first in your chosen J2EE server.

Resources

Dmitri Maximovich is an independent consultant specializing in software design and development.


Return to ONJava.com.