Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Bitronix Transaction Manager
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h1>How to use BTM as the transaction manager with Spring 2.5.x</h1> <p>These instructions have been verified against BTM 1.3.2.</p> <h4>Contents</h4> <img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e3RvYzptYXhMZXZlbD0zfG1pbkxldmVsPTJ9&locale=en_GB&version=2" data-macro-name="toc" data-macro-parameters="maxLevel=3|minLevel=2"> <h2>Step 1: Copy the BTM jars</h2> <p>Include the following jars from the BTM distribution into your classpath:</p> <ul> <li>btm-1.3.2.jar</li> <li>geronimo-jta_1.0.1B_spec-1.0.1.jar</li> <li>geronimo-jms_1.1_spec-1.0.1.jar (<em>only if you are going to use JMS</em>)</li> <li>slf4j-api-1.5.2.jar</li> <li>slf4j-jdk14-1.5.2.jar (<em>or any other one available <a class="confluence-link" href="/display/BTM/DebugLogging13" data-linked-resource-id="102105614" data-linked-resource-type="page" data-linked-resource-default-alias="DebugLogging13" data-base-url="http://docs.codehaus.org">here</a></em>)</li> </ul> <h2>Step 2: Configure JDBC datasource beans</h2> <p>The first things you will need to configure are the JDBC datasources.</p> <p>Here is a sample bean configuration using Embedded Derby:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="XML" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6WE1MfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <bean id="derbyDataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource" init-method="init" destroy-method="close"> <property name="className" value="org.apache.derby.jdbc.EmbeddedXADataSource" /> <property name="uniqueName" value="derbydb" /> <property name="maxPoolSize" value="5" /> <property name="driverProperties"> <props> <prop key="databaseName">derbydb</prop> </props> </property> </bean> </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="info" data-macro-parameters="title=API-created datasources" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2luZm86dGl0bGU9QVBJLWNyZWF0ZWQgZGF0YXNvdXJjZXN9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>Since the datasources are created via the BTM API (ie: not with <code>ResourceLoader</code>) it is up to the API user to manage their lifecycle, mainly calling <code>init()</code> before usage and <code>close()</code> at shutdown.<br /> This is why the two <code>init-method</code> and <code>destroy-method</code> attributes are set: to have Spring take care of that lifecycle.</p></td></tr></table> <h2>Step 3: Configure JMS connection factory beans</h2> <p>The next things you will need to configure are the JMS connection factories.</p> <p>Here is a sample bean configuration using ActiveMQ:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="XML" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6WE1MfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <bean id="connectionFactory" class="bitronix.tm.resource.jms.PoolingConnectionFactory" init-method="init" destroy-method="close"> <property name="className" value="org.apache.activemq.ActiveMQXAConnectionFactory" /> <property name="uniqueName" value="activemq" /> <property name="maxPoolSize" value="3" /> <property name="driverProperties"> <props> <prop key="brokerURL">vm://localhost</prop> </props> </property> </bean> </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="info" data-macro-parameters="title=API-created connection factories" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2luZm86dGl0bGU9QVBJLWNyZWF0ZWQgY29ubmVjdGlvbiBmYWN0b3JpZXN9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>Since the connection factories are created via the BTM API (ie: not with <code>ResourceLoader</code>) it is up to the API user to manage their lifecycle, mainly calling <code>init()</code> before usage and <code>close()</code> at shutdown.<br /> This is why the two <code>init-method</code> and <code>destroy-method</code> attributes are set: to have Spring take care of that lifecycle.</p></td></tr></table> <h2>Step 4: Configure BTM beans</h2> <p>The next thing you need to do is configure beans for BTM.</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="XML" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6WE1MfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <!-- Bitronix Transaction Manager embedded configuration --> <bean id="btmConfig" factory-method="getConfiguration" class="bitronix.tm.TransactionManagerServices"> <property name="serverId" value="spring-btm" /> </bean> <!-- create BTM transaction manager --> <bean id="BitronixTransactionManager" factory-method="getTransactionManager" class="bitronix.tm.TransactionManagerServices" depends-on="btmConfig" destroy-method="shutdown" /> </pre></td></tr></table> <h2>Step 5: Configure Spring PlatformTransactionManager</h2> <p>Next, you need to create a Spring <a href="http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/transaction/PlatformTransactionManager.html">PlatformTransactionManager</a>. There are many of them but the one we are interested in is the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/transaction/jta/JtaTransactionManager.html">JtaTransactionManager</a>. This is required as Spring internally uses <code>PlatformTransactionManager</code> for all transactional work.</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="XML" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6WE1MfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <!-- Spring JtaTransactionManager --> <bean id="JtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="BitronixTransactionManager" /> <property name="userTransaction" ref="BitronixTransactionManager" /> </bean> </pre></td></tr></table> <p>This is really all you need to get JTA support with BTM inside Spring. You could directly make use of the <code>JtaTransactionManager</code> bean in your code but there are more elegant solutions: using Sping's AOP support to get declarative transaction management.</p> <h2>Step 6: Configure declarative transaction management</h2> <p>This can easily be achieved thanks to Spring's <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/transaction/interceptor/TransactionProxyFactoryBean.html">TransactionProxyFactoryBean</a>.</p> <p>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.</p> <p>Here is short example:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="XML" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6WE1MfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <bean id="MyObjectFacade" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="JtaTransactionManager" /> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED, -Exception</prop> </props> </property> <property name="target" ref="MyObject" /> </bean> </pre></td></tr></table> <p>This expects a <code>MyObject</code> bean to also be configured. You should then make use of the <code>MyObjectFacade</code> bean that will start a new transaction on any method call if no transaction is already running (the <code><prop key="*">PROPAGATION_REQUIRED</code> piece), commit the transaction when the method returns or rollback the transaction if any exception is thrown (the <code>, -Exception</prop></code> piece).</p> <p>If you need more details on what can be done and how things are working refer to the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/transaction/interceptor/TransactionProxyFactoryBean.html">TransactionProxyFactoryBean</a> class javadoc.<br /> <img class="confluence-embedded-image confluence-external-resource" src="http://www.bitronix.be/images/shim.gif" data-image-src="http://www.bitronix.be/images/shim.gif"></p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced