Help, The JTA doesn't work!
w19995 Aug 3, 2006 11:16 PMI'm now testing the example: JDBCTest in Chapter 6 of JBoss JTA Programmers Guide. But it doesn't work and no any Exception throws. I use Oracle 8.1.7 and JDK 1.5.
Does anyone can help me? Thanks.
The source code:
package demo; import java.sql.*; import java.util.*; import com.arjuna.ats.jdbc.*; public class JDBCTest { public static void main(String[] args) { /* * Register the driver via the system properties variable * "jdbc.drivers" */ Properties p = System.getProperties(); p.put("jdbc.drivers", "oracle.jdbc.driver.OracleDriver"); System.setProperties(p); /* * Now register the JBossJTA 2.x transactional driver. */ try { DriverManager.registerDriver(new com.arjuna.ats.jdbc. TransactionalDriver()); } catch (Exception e) { e.printStackTrace(); System.exit(0); } Connection conn = null; Connection conn2 = null; Statement stmt = null; // non-tx statement Statement stmtx = null; // will be a tx-statement Properties dbProperties = new Properties(); String url = "jdbc:oracle:thin:@172.21.172.204:1521:inms05"; try { System.out.println("\nCreating connection to database: " + url); dbProperties.put(TransactionalDriver.userName, "upm30415"); dbProperties.put(TransactionalDriver.password, "upm30415"); dbProperties.put(TransactionalDriver.dynamicClass, "com.arjuna.ats.internal.jdbc.drivers.oracle_8_1_6"); conn = DriverManager.getConnection(url, dbProperties); conn2 = DriverManager.getConnection(url, dbProperties); stmt = conn.createStatement(); // non-tx statement try { stmt.executeUpdate("DROP TABLE test_table"); stmt.executeUpdate("DROP TABLE test_table2"); } catch (Exception e) { // assume not in database. } try { stmt.executeUpdate( "CREATE TABLE test_table (a INTEGER,b INTEGER)"); stmt.executeUpdate( "CREATE TABLE test_table2 (a INTEGER,b INTEGER)"); } catch (Exception e) { } try { System.out.println("Starting top-level transaction."); com.arjuna.ats.jta.UserTransaction.userTransaction().begin(); stmtx = conn.createStatement(); // will be a tx-statement System.out.println("\nAdding entries to table 1."); stmtx.executeUpdate( "INSERT INTO test_table (a, b) VALUES(1, 2)"); ResultSet res1 = null; System.out.println("\nInspecting table 1."); res1 = stmtx.executeQuery("SELECT * FROM test_table"); while (res1.next()) { System.out.println("Column 1: " + res1.getInt(1)); System.out.println("Column 2: " + res1.getInt(2)); } System.out.println("\nAdding entries to table 2."); stmtx.executeUpdate( "INSERT INTO test_table2 (a, b) VALUES(3, 4)"); res1 = stmtx.executeQuery( "SELECT * FROM test_table2"); System.out.println( "\nInspecting table 2."); while (res1.next()) { System.out.println("Column 1: " + res1.getInt(1)); System.out.println("Column 2: " + res1.getInt(2)); } System.out.print("\nNow attempting to rollback changes."); com.arjuna.ats.jta.UserTransaction.userTransaction(). rollback(); com.arjuna.ats.jta.UserTransaction.userTransaction().begin(); stmtx = conn.createStatement(); ResultSet res2 = null; System.out.println("\nNow checking state of table 1."); res2 = stmtx.executeQuery("SELECT * FROM test_table"); while (res2.next()) { System.out.println("Column 1: " + res2.getInt(1)); System.out.println("Column 2: " + res2.getInt(2)); } System.out.println("\nNow checking state of table 2."); stmtx = conn.createStatement(); res2 = stmtx.executeQuery("SELECT * FROM test_table2"); while (res2.next()) { System.out.println("Column 1: " + res2.getInt(1)); System.out.println("Column 2: " + res2.getInt(2)); } com.arjuna.ats.jta.UserTransaction.userTransaction().commit(); } catch (Exception ex) { ex.printStackTrace(); System.exit(0); } } catch (Exception sysEx) { sysEx.printStackTrace(); System.exit(0); } } }