Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
package jtatest;

import bitronix.tm.BitronixTransactionManager;
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jdbc.PoolingDataSource;

import javax.transaction.UserTransaction;
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();

        UserTransactionBitronixTransactionManager utbtm = TransactionManagerServices.getTransactionManager();

        utbtm.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();
            utbtm.commit();
        } catch (SQLException ex) {
            ex.printStackTrace();
            utbtm.rollback();
        }

        utbtm.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();
            utbtm.commit();
        } catch (SQLException ex) {
            ex.printStackTrace();
            utbtm.rollback();
        }

        derby1Ds.close();
        
        btm.shutdown();
    }
}

This code expects a Derby database named db1 to be already created in the current folder with this table:

...

And this is what the application outputs when you run it:

Code Block
AugFeb 115, 20072009 101:4837:0301 AMPM bitronix.tm.BitronixTransactionManager logVersion
INFO: Bitronix Transaction Manager version 1.3.2
AugFeb 115, 2007 10:48:03 AM bitronix.tm.Configuration <init>
INFO: loading default configuration
Aug 1, 2007 10:48:03 AM2009 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!).
AugFeb 115, 20072009 101:4837:0301 AMPM bitronix.tm.Configuration buildServerIdArray
INFO: JVM unique ID: <1<85.226.392.4>135>
AugFeb 115, 20072009 101:4837:0302 AMPM 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!

AugFeb 115, 20072009 101:4837:0403 AMPM bitronix.tm.BitronixTransactionManager shutdown
INFO: shutting down Bitronix Transaction Manager

...

You can download this demo here: newUserDemo.zip.

There is an ant build.xml file included as well as a the necessary batch and shell scripts required to run the application from Windows or Unix.

...

Here is the list of JAR files with version required to run this demo. They're all included in the downloadable ZIP file.

JAR name

Version

btm-1.3.2.jar

BTM 1.3.2

geronimo-spec-jta-_1.0.1B_spec-rc41.0.1.jar

BTM 1.3.2

slf4j-api-1.45.32.jar

SLF4J 1.45.3 2

slf4j-jdk14-1.45.32.jar

SLF4J 1.45.3 2

derby-10.2.2.0.jar

Derby 10.2.2.0

derbytools-10.2.2.0.jar

Derby 10.2.2.0

...