Skip to end of metadata
Go to start of metadata

How to use BTM as the transaction manager with Spring 2.5.x

These instructions have been verified against BTM 1.3.2.

Contents

Step 1: Copy the BTM jars

Include the following jars from the BTM distribution into your classpath:

  • btm-1.3.2.jar
  • geronimo-jta_1.0.1B_spec-1.0.1.jar
  • geronimo-jms_1.1_spec-1.0.1.jar (only if you are going to use JMS)
  • slf4j-api-1.5.2.jar
  • slf4j-jdk14-1.5.2.jar (or any other one available here)

Step 2: Configure JDBC datasource beans

The first things you will need to configure are the JDBC datasources.

Here is a sample bean configuration using Embedded Derby:

API-created datasources

Icon

Since the datasources are created via the BTM API (ie: not with ResourceLoader) it is up to the API user to manage their lifecycle, mainly calling init() before usage and close() at shutdown.
This is why the two init-method and destroy-method attributes are set: to have Spring take care of that lifecycle.

Step 3: Configure JMS connection factory beans

The next things you will need to configure are the JMS connection factories.

Here is a sample bean configuration using ActiveMQ:

API-created connection factories

Icon

Since the connection factories are created via the BTM API (ie: not with ResourceLoader) it is up to the API user to manage their lifecycle, mainly calling init() before usage and close() at shutdown.
This is why the two init-method and destroy-method attributes are set: to have Spring take care of that lifecycle.

Step 4: Configure BTM beans

The next thing you need to do is configure beans for BTM.

Step 5: Configure Spring PlatformTransactionManager

Next, you need to create a Spring PlatformTransactionManager. There are many of them but the one we are interested in is the JtaTransactionManager. This is required as Spring internally uses PlatformTransactionManager for all transactional work.

This is really all you need to get JTA support with BTM inside Spring. You could directly make use of the JtaTransactionManager bean in your code but there are more elegant solutions: using Sping's AOP support to get declarative transaction management.

Step 6: Configure declarative transaction management

This can easily be achieved thanks to Spring's TransactionProxyFactoryBean.

The idea behind it is to wrap your bean with a Spring-generated proxy that will intercept calls and perform transaction management according to a configuration.

Here is short example:

This expects a MyObject bean to also be configured. You should then make use of the MyObjectFacade bean that will start a new transaction on any method call if no transaction is already running (the <prop key="*">PROPAGATION_REQUIRED piece), commit the transaction when the method returns or rollback the transaction if any exception is thrown (the , -Exception</prop> piece).

If you need more details on what can be done and how things are working refer to the TransactionProxyFactoryBean class javadoc.

  • No labels

3 Comments

  1. Annex

    Spring - Tomcat integration

    When BTM is declared at Tomcat level (see Tomcat ), getting from JNDI registry is as simple as:

    The JtaTransactionManager looks for it with JNDI names: java:UserTransaction or java:comp/UserTransaction

    Spring - Hibernate integration

    There is no need to define the following Hibernate properties

    Because when the jtaTransactionManager property is set on the SessionFactoryBean, Spring automatically register its own hooks:

  2. When I test according to your guidance, I faced an error like that : “Caused by: java.lang.IllegalStateException: Cannot convert value of type [bitronix.tm.BitronixTransactionManager] to required type [javax.transaction.TransactionManager] for property 'transactionManager': no matching editors or conversion strategy found”. Is there any way to solve the problem?

  3. That's a classloader issue. The most common cause is that you've embedded the jta jar in your web app. Make sure there is no trace of jta.jar in your webapp but that there's a copy in Tomcat's lib folder.