1 28 29 import java.sql.Connection ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.Statement ; 33 34 import javax.naming.InitialContext ; 35 import javax.sql.DataSource ; 36 import javax.transaction.UserTransaction ; 37 38 41 public class DistributedDataSourceExample { 42 43 private static final String SQL_REQUEST = "select id, foo from testdata"; 44 45 private static final String SQL_QUERY = 46 "update testdata set foo = ? where id=1"; 47 48 private static Connection conn; 49 50 private static void updateTable(int newValue) { 51 try { 52 Statement stmt = conn.createStatement(); 53 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 54 pstmt.setInt(1, newValue); 55 pstmt.executeUpdate(); 56 } catch (Exception e) { 57 e.printStackTrace(); 58 } 59 } 60 61 private static void printTable() { 62 try { 63 Statement stmt = conn.createStatement(); 64 ResultSet rset = stmt.executeQuery(SQL_REQUEST); 65 int numcols = rset.getMetaData().getColumnCount(); 66 for (int i = 1; i <= numcols; i++) { 67 System.out.print("\t" + rset.getMetaData().getColumnName(i)); 68 } 69 System.out.println(); 70 while (rset.next()) { 71 for (int i = 1; i <= numcols; i++) { 72 System.out.print("\t" + rset.getString(i)); 73 } 74 System.out.println(""); 75 } 76 } catch (Exception e) { 77 e.printStackTrace(); 78 } 79 } 80 81 public static void main(String [] args) { 82 83 if (args.length != 2|| (!args[0].equals("commit") && !args[0].equals("rollback"))) { 84 System.err.println("usage: java DistributedDataSourceExample [completion] [number]\n"); 85 System.exit(1); 86 } 87 88 String completion = args[0]; 89 int value = 0; 90 try { 91 value = Integer.parseInt(args[1]); 92 } catch (NumberFormatException e) { 93 System.out.println("[number] has to be an integer\n"); 94 System.exit(1); 95 } 96 97 UserTransaction utx = null; 98 try { 99 System.out.println("create initial context"); 100 InitialContext ictx = new InitialContext (); 101 System.out.println("lookup UserTransaction at : UserTransaction"); 102 utx = (UserTransaction ) ictx.lookup("UserTransaction"); 103 System.out.println("lookup DataSource at: DataSource"); 104 DataSource ds = (DataSource ) ictx.lookup("DataSource"); 105 System.out.println("get a connection"); 106 conn = ds.getConnection("mojo", "jojo"); 107 } catch (Exception e) { 108 System.out.println( 109 "Exception of type :" 110 + e.getClass().getName() 111 + " has been thrown"); 112 System.out.println("Exception message :" + e.getMessage()); 113 e.printStackTrace(); 114 System.exit(1); 115 } 116 117 System.out.println("before transaction, table is:"); 118 printTable(); 119 120 try { 121 System.out.println("begin a transaction"); 122 utx.begin(); 123 124 System.out.println("update the table"); 125 updateTable(value); 126 127 if (completion.equals("commit")) { 128 System.out.println("*commit* the transaction"); 129 utx.commit(); 130 } else { 131 System.out.println("*rollback* the transaction"); 132 utx.rollback(); 133 } 134 } catch (Exception e) { 135 System.out.println( 136 "Exception of type :" 137 + e.getClass().getName() 138 + " has been thrown"); 139 System.out.println("Exception message :" + e.getMessage()); 140 e.printStackTrace(); 141 System.exit(1); 142 } 143 144 System.out.println("after transaction, table is:"); 145 printTable(); 146 System.out.println("Recovery Example is ok.\n"); 147 System.exit(0); 148 } 149 } 150
| Popular Tags
|