...
Let's see what JTA hello, world! 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:
| Code Block |
|---|
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();
}
}
|
This code expects a Derby database named db1 to be already created in the current folder with this table:
| Code Block | ||
|---|---|---|
| ||
create table messages (
content varchar(50)
);
|
And this is what the application outputs when you run it:
| Code Block |
|---|
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: <85.26.92.135>
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
|
...