1 2 import java.sql.Connection ; 3 import java.sql.PreparedStatement ; 4 import java.sql.ResultSet ; 5 6 import javax.naming.Context ; 7 import javax.naming.InitialContext ; 8 import javax.transaction.UserTransaction ; 9 10 public class StandardXAPoolDataSourceExample { 11 12 private static final String USAGE = "usage: java StandardXAPoolDataSourceExample [commit|rollback] [number]"; 13 private static final String SQL_REQUEST = "select id, foo from testdata"; 14 private static final String SQL_QUERY = "update testdata set foo = ? where id=1"; 15 private static final String USER_TRANSACTION_JNDI_NAME = "UserTransaction"; 16 private static Connection conn = null; 17 18 private static void printTable() { 19 try { 20 PreparedStatement pstmt = conn.prepareStatement(SQL_REQUEST); 21 ResultSet rset = pstmt.executeQuery(); 22 int numcols = rset.getMetaData().getColumnCount(); 23 for (int i = 1; i <= numcols; i++) { 24 System.out.print("\t" + rset.getMetaData().getColumnName(i)); 25 } 26 System.out.println(); 27 while (rset.next()) { 28 for (int i = 1; i <= numcols; i++) { 29 System.out.print("\t" + rset.getString(i)); 30 } 31 System.out.println(""); 32 } 33 } catch (Exception e) { 34 e.printStackTrace(); 35 } 36 } 37 38 private static void updateTable(int newValue) { 39 try { 40 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 41 pstmt.setInt(1, newValue); 42 pstmt.executeUpdate(); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } 46 } 47 48 public static void main(String [] args) { 49 50 if (args.length != 2|| (!args[0].equals("commit") && !args[0].equals("rollback"))) { 51 System.err.println(USAGE + "\n"); 52 System.exit(1); 53 } 54 55 String completion = args[0]; 56 57 int newValue = 0; 58 try { 59 newValue = Integer.parseInt(args[1]); 60 } catch (NumberFormatException e) { 61 System.err.println(USAGE); 62 System.err.println("[number] has to be an integer\n"); 63 System.exit(1); 64 } 65 66 System.out.println("start server"); 67 DatabaseHelper dbHelper = new DatabaseHelper(); 68 69 UserTransaction utx = null; 70 try { 71 System.out.println("create initial context"); 72 Context ictx = new InitialContext (); 73 System.out.println("lookup UserTransaction at : " + USER_TRANSACTION_JNDI_NAME); 74 utx = (UserTransaction ) ictx.lookup(USER_TRANSACTION_JNDI_NAME); 75 } catch (Exception e) { 76 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 77 System.err.println("Exception message :" + e.getMessage()); 78 e.printStackTrace(); 79 System.exit(1); 80 } 81 82 try { 83 System.out.println("get a connection"); 84 conn = dbHelper.getConnection(); 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 89 System.out.println("before transaction, table is:"); 90 printTable(); 91 92 try { 93 System.out.println("begin a transaction"); 94 utx.begin(); 95 96 System.out.println("update the table"); 97 updateTable(newValue); 98 99 if (completion.equals("commit")) { 100 System.out.println("*commit* the transaction"); 101 utx.commit(); 102 } else { 103 System.out.println("*rollback* the transaction"); 104 utx.rollback(); 105 } 106 } catch (Exception e) { 107 System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 108 System.out.println("Exception message :" + e.getMessage()); 109 e.printStackTrace(); 110 System.exit(1); 111 } 112 113 utx = null; 114 115 System.out.println("after transaction, table is:"); 116 printTable(); 117 118 try { 119 System.out.println("close connection"); 120 conn.close(); 121 122 } catch (Exception e) { 123 e.printStackTrace(); 124 } finally { 125 conn = null; 126 } 127 128 System.out.println("stop server"); 129 dbHelper.stop(); 130 131 System.out.println("JDBC example is ok.\n"); 132 System.exit(0); 133 } 134 } 135 | Popular Tags |