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
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>New user's guide to BTM and JTA</h1><p>JTA has the reputation of being hard to use and understand. This isn't true. There are a few new things you need to know but in general this information adds on top of your current JDBC or JMS experience.</p><p>Let's see what JTA <em>hello, world!</em> using BTM looks like. Here is a complete application's code that inserts a row in a Derby database using a JTA transaction and reads it back in another transaction:</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>package jtatest; import bitronix.tm.BitronixTransactionManager; import bitronix.tm.TransactionManagerServices; import bitronix.tm.resource.jdbc.PoolingDataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.ResultSet; public class Test { public static void main(String[] args) throws Exception { String who = "world"; if (args.length > 0) who = args[0]; PoolingDataSource derby1Ds = new PoolingDataSource(); derby1Ds.setClassName("org.apache.derby.jdbc.EmbeddedXADataSource"); derby1Ds.setUniqueName("derby1"); derby1Ds.setMaxPoolSize(3); derby1Ds.getDriverProperties().setProperty("databaseName", "db1"); derby1Ds.init(); BitronixTransactionManager btm = TransactionManagerServices.getTransactionManager(); btm.begin(); try { Connection c = derby1Ds.getConnection(); PreparedStatement stmt = c.prepareStatement("insert into messages(content) values (?)"); stmt.setString(1, "hello, " + who + "!"); stmt.executeUpdate(); stmt.close(); c.close(); btm.commit(); } catch (SQLException ex) { ex.printStackTrace(); btm.rollback(); } btm.begin(); try { Connection c = derby1Ds.getConnection(); PreparedStatement stmt = c.prepareStatement("select content from messages"); ResultSet rs = stmt.executeQuery(); while(rs.next()) System.out.println(rs.getString(1)); rs.close(); stmt.close(); c.close(); btm.commit(); } catch (SQLException ex) { ex.printStackTrace(); btm.rollback(); } derby1Ds.close(); btm.shutdown(); } } </pre></td></tr></table><p>This code expects a Derby database named <strong>db1</strong> to be already created in the current folder with this table:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="sql" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6c3FsfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>create table messages ( content varchar(50) ); </pre></td></tr></table><p>And this is what the application outputs when you run it:</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>Feb 15, 2009 1:37:01 PM bitronix.tm.BitronixTransactionManager logVersion INFO: Bitronix Transaction Manager version 1.3.2 Feb 15, 2009 1:37:01 PM bitronix.tm.Configuration buildServerIdArray WARNING: cannot get this JVM unique ID. Make sure it is configured and you only use ASCII characters. Will use IP address instead (unsafe for production usage!). Feb 15, 2009 1:37:01 PM bitronix.tm.Configuration buildServerIdArray INFO: JVM unique ID: <x.x.x.x> Feb 15, 2009 1:37:02 PM bitronix.tm.recovery.Recoverer run INFO: recovery committed 0 dangling transaction(s) and rolled back 0 aborted transaction(s) on 1 resource(s) [derby1] hello, world! Feb 15, 2009 1:37:03 PM bitronix.tm.BitronixTransactionManager shutdown INFO: shutting down Bitronix Transaction Manager </pre></td></tr></table><p>Neat, huh? Who said using JTA is complex?</p><h3>Download</h3><p>You can download this demo here: <a href="http://www.bitronix.be/examples/newUserDemo.zip">newUserDemo.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 a directory called <strong>db1</strong>. Then you can start the demo by either running <strong>run.sh</strong> or <strong>run.bat</strong>.</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.2.jar</p></td><td class="confluenceTd"><p>BTM 1.3.2</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.2</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.2.2.0.jar</p></td><td class="confluenceTd"><p>Derby 10.2.2.0</p></td></tr><tr><td class="confluenceTd"><p>derbytools-10.2.2.0.jar</p></td><td class="confluenceTd"><p>Derby 10.2.2.0</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