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>Integrating BTM with iBatis</h1> <p>iBatis can be integrated straight with any JTA transaction manager. These instructions have been verified against BTM 1.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>JTA datasources</h2> <p>iBatis cannot directly create a BTM <code>PoolingDataSource</code>. You will have to create them yourself (either via the API or the Resource Loader) and make sure they are bound to a JNDI server.</p> <h3>Setting up the BTM JNDI server</h3> <p>You have to bind the datasources and the transaction manager to some JNDI server. You can use any one you wish, but BTM 1.3 ships with one you might find more convenient to use.</p> <p>It is very easy to use it in a standalone J2SE application. Just create a jndi.properties file at the root of your classpath. It should only contain this line:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory </pre></td></tr></table> <p>You can now just create a <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/naming/InitialContext.html">InitialContext</a> with the no-args constructor to have access to it.</p> <h3>API way: Creating the datasources</h3> <p>As you can expect, you will need to create one <code>PoolingDataSource</code> per database. Say that you want to use two Embedded Derby databases, and configure them via the BTM API. Here is what your code would look like:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> PoolingDataSource ds1 = new PoolingDataSource(); ds1.setUniqueName("jdbc/testDS1"); ds1.setClassName("org.apache.derby.jdbc.EmbeddedXADataSource"); ds1.setMaxPoolSize(3); ds1.getDriverProperties().put("databaseName", "users1"); ds1.init(); PoolingDataSource ds2 = new PoolingDataSource(); ds2.setUniqueName("jdbc/testDS2"); ds2.setClassName("org.apache.derby.jdbc.EmbeddedXADataSource"); ds2.setMaxPoolSize(3); ds2.getDriverProperties().put("databaseName", "users2"); ds2.init(); </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="tip" data-macro-parameters="title=Datasource's unique name and JNDI location correspondence" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3RpcDp0aXRsZT1EYXRhc291cmNlJ3MgdW5pcXVlIG5hbWUgYW5kIEpOREkgbG9jYXRpb24gY29ycmVzcG9uZGVuY2V9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>The BTM JNDI provider will automatically bind the datasources under their unique name. In this case, you can look up jdbc/testDS1 or jdbc/testDS2 as soon as the transaction manager started without having anything else to configure.</p></td></tr></table> <p>Finally, here is the code to bind the datasources:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Context ctx = new InitialContext(); ctx.createSubcontext("jdbc"); ctx.rebind("jdbc/testDS1", ds1); ctx.rebind("jdbc/testDS2", ds2); ctx.close(); </pre></td></tr></table> <h3>Resource Loader way: Creating the datasources</h3> <p>You can use BTM's Resource Loader instead of the BTM API. It is usually a good idea when you want to create a fully standalone application as you can get rid of the datasources creation, JNDI binding and shutdown code.</p> <p>Create a <code>datasources.properties</code> file in the current directory containing these properties:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> resource.ds1.className=org.apache.derby.jdbc.EmbeddedXADataSource resource.ds1.uniqueName=jdbc/testDS1 resource.ds1.maxPoolSize=3 resource.ds1.driverProperties.databaseName=users1 resource.ds2.className=org.apache.derby.jdbc.EmbeddedXADataSource resource.ds2.uniqueName=jdbc/testDS2 resource.ds2.maxPoolSize=3 resource.ds2.driverProperties.databaseName=users2 </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="tip" data-macro-parameters="title=Resource Loader JNDI binding" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3RpcDp0aXRsZT1SZXNvdXJjZSBMb2FkZXIgSk5ESSBiaW5kaW5nfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>As with the API, the datasources will be available in JNDI under their unique name.</p></td></tr></table> <p>In your application code, you will have to configure BTM to use the resource loader:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> TransactionManagerServices.getConfiguration().setResourceConfigurationFilename("./datasources.properties"); userTransaction = TransactionManagerServices.getTransactionManager(); </pre></td></tr></table> <p>This has the exact same behavior as creating the <code>PoolingDataSource</code> objects and binding them to JNDI yourself. It is just more convenient.</p> <h2>iBatis SqlMapClients</h2> <p>You need to configure exactly one <code>SqlMapClient</code> per datasource.</p> <h3>Datasource JNDI location</h3> <p>You have to tell iBatis where to get the BTM datasource via JNDI. Add a <code>DataSource</code> property to the <code>dataSource</code> tag and set its value to the JNDI location of your datasource:</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=e2NvZGU6eG1sfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <dataSource type="JNDI"> <property name="DataSource" value="jdbc/testDS1"/> </dataSource> </pre></td></tr></table> <h3>Transaction Manager JNDI location</h3> <p>You have to set <code>transactionManager</code> tag's type attribute to <code>JTA</code> and wrap the <code>dataSource</code> tag in it.</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=e2NvZGU6eG1sfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <transactionManager type="JTA" > <property name="UserTransaction" value="UserTransaction"/> <dataSource type="JNDI"> <property name="DataSource" value="jdbc/testDS1"/> </dataSource> </transactionManager> </pre></td></tr></table> <h3>SqlMapConfig XML configuration files</h3> <p>Here is what the <code>SqlMapConfig1.xml</code> file will look like for the first datasource.</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> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <transactionManager type="JTA" > <property name="UserTransaction" value="UserTransaction"/> <dataSource type="JNDI"> <property name="DataSource" value="jdbc/testDS1"/> </dataSource> </transactionManager> <sqlMap resource="bitronix/examples/ibatis/entities/User.xml"/> </sqlMapConfig> </pre></td></tr></table> <p>And here is the <code>SqlMapConfig2.xml</code> for the second datasource:</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> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <transactionManager type="JTA" > <property name="UserTransaction" value="UserTransaction"/> <dataSource type="JNDI"> <property name="DataSource" value="jdbc/testDS2"/> </dataSource> </transactionManager> <sqlMap resource="bitronix/examples/ibatis/entities/User.xml"/> </sqlMapConfig> </pre></td></tr></table> <h2>End result</h2> <p>Now that iBatis and BTM are properly configured, you can simply use the JTA and iBatis APIs in your application.</p> <h3>Application code</h3> <p>Here is what your code will look like when you want to update the content of both databases atomically:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> for (int i=0; i<10 ;i++) { System.out.println("Iteration #" + (i+1)); userTransaction.setTransactionTimeout(60); userTransaction.begin(); try { System.out.println("*** DB1 ***"); persistUser(smc1, "user"); listUsers(smc1); System.out.println("*** DB2 ***"); persistUser(smc2, "user"); listUsers(smc2); userTransaction.commit(); } catch (Exception ex) { ex.printStackTrace(); userTransaction.rollback(); } } </pre></td></tr></table> <p>Say that <code>persistUser()</code> creates a new user, in <em>no way</em> will a user be created in one database and not in the other.</p> <h2>Download</h2> <p>You can download a sample runnable application putting these explanations in practice. It contains all the code that has been skipped for clarity in this page. Both the API and Resource Loader ways are implemented so you can try both and see which one you prefer.</p> <p>You can download this demo here: <a href="http://www.bitronix.be/examples/iBatisBTM13.zip">iBatisBTM13.zip</a>.</p> <p>There is an ant <strong>build.xml</strong> file included as well as a the necessary batch and shell scripts required to run the application from Windows or Unix.</p> <p>Before you run the application, you have to create the Derby database. Just run the included <strong>derby-create.sh</strong> or <strong>derby-create.bat</strong> script to do so, it will create two directories called <strong>users1</strong> and <strong>users2</strong>. Then you can start the demo by either running <strong>run_api.sh</strong> or <strong>run_api.bat</strong> for the API version, <strong>run_rl.sh</strong> or <strong>run_rl.bat</strong> for the Resource Loader version.</p> <p>Here is the list of JAR files with version required to run this demo. They're all included in the downloadable ZIP file.</p> <table class="confluenceTable"><tbody> <tr> <th class="confluenceTh"><p> JAR name </p></th> <th class="confluenceTh"><p> Version </p></th> </tr> <tr> <td class="confluenceTd"><p> btm-1.3.jar </p></td> <td class="confluenceTd"><p> BTM 1.3 </p></td> </tr> <tr> <td class="confluenceTd"><p> geronimo-jta_1.0.1B_spec-1.0.1.jar </p></td> <td class="confluenceTd"><p> BTM 1.3 </p></td> </tr> <tr> <td class="confluenceTd"><p> slf4j-api-1.5.2.jar </p></td> <td class="confluenceTd"><p> SLF4J 1.5.2 </p></td> </tr> <tr> <td class="confluenceTd"><p> slf4j-jdk14-1.5.2.jar </p></td> <td class="confluenceTd"><p> SLF4J 1.5.2 </p></td> </tr> <tr> <td class="confluenceTd"><p> derby-10.3.1.4.jar </p></td> <td class="confluenceTd"><p> Derby 10.3.1.4 </p></td> </tr> <tr> <td class="confluenceTd"><p> derbytools-10.3.1.4.jar </p></td> <td class="confluenceTd"><p> Derby 10.3.1.4 </p></td> </tr> <tr> <td class="confluenceTd"><p> commons-logging-1.0.4.jar </p></td> <td class="confluenceTd"><p> Apache Commons Logging 1.0.4 </p></td> </tr> <tr> <td class="confluenceTd"><p> ibatis-2.3.3.720.jar </p></td> <td class="confluenceTd"><p> iBatis 2.3.3 </p></td> </tr> </tbody></table> <p><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