1 44 45 import java.sql.Connection ; 46 import java.sql.PreparedStatement ; 47 import java.sql.ResultSet ; 48 import java.sql.Statement ; 49 50 import javax.naming.Context ; 51 import javax.naming.InitialContext ; 52 import javax.transaction.UserTransaction ; 53 54 60 public class JdbcDistExample { 61 62 private static final String USAGE = "usage: java JdbcDistExample [database1 database2] [commit|rollback] [number]"; 63 private static final String SQL_REQUEST1 = "select id, foo from testdata"; 64 private static final String SQL_QUERY1 = "update testdata set foo = ? where id=1"; 65 private static final String SQL_REQUEST2 = "select id, foo from testdata"; 66 private static final String SQL_QUERY2 = "update testdata set foo = ? where id=1"; 67 private static final String USER_TRANSACTION_JNDI_NAME = "UserTransaction"; 68 private static Connection conn1 = null; 69 private static Connection conn2 = null; 70 71 private static void printTable(Connection conn, String sqlRequest) { 72 try { 73 Statement stmt = conn.createStatement(); 74 ResultSet rset = stmt.executeQuery(sqlRequest); 75 int numcols = rset.getMetaData().getColumnCount(); 76 for (int i = 1; i <= numcols; i++) { 77 System.out.print("\t" + rset.getMetaData().getColumnName(i)); 78 } 79 System.out.println(); 80 while (rset.next()) { 81 for (int i = 1; i <= numcols; i++) { 82 System.out.print("\t" + rset.getString(i)); 83 } 84 System.out.println(""); 85 } 86 } catch (Exception e) { 87 e.printStackTrace(); 88 } 89 } 90 91 private static void updateTable(Connection conn, String sqlQuery, int newValue) { 92 try { 93 PreparedStatement pstmt = conn.prepareStatement(sqlQuery); 94 pstmt.setInt(1, newValue); 95 pstmt.executeUpdate(); 96 } catch (Exception e) { 97 e.printStackTrace(); 98 } 99 } 100 101 public static void main(String [] args) { 102 103 if (args.length != 4 || (!args[2].equals("commit") && !args[2].equals("rollback"))) { 104 System.out.println(USAGE + "\n"); 105 System.exit(1); 106 } 107 108 String completion = args[2]; 109 110 int newValue = 0; 111 try { 112 newValue = Integer.parseInt(args[3]); 113 } catch (NumberFormatException e) { 114 System.out.println(USAGE); 115 System.out.println("[number] has to be an integer\n"); 116 System.exit(1); 117 } 118 119 System.out.println("start server"); 120 DistDatabaseHelper ddbHelper = new DistDatabaseHelper(args[0], args[1]); 121 122 UserTransaction utx = null; 123 try { 124 System.out.println("create initial context"); 125 Context ictx = new InitialContext (); 126 System.out.println("lookup UserTransaction at : " + USER_TRANSACTION_JNDI_NAME); 127 utx = (UserTransaction ) ictx.lookup(USER_TRANSACTION_JNDI_NAME); 128 } catch (Exception e) { 129 System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 130 System.out.println("Exception message :" + e.getMessage()); 131 e.printStackTrace(); 132 System.exit(1); 133 } 134 135 try { 136 System.out.println("get a connection"); 137 conn1 = ddbHelper.getConnection1(); 138 conn2 = ddbHelper.getConnection2(); 139 } catch (Exception e) { 140 e.printStackTrace(); 141 } 142 143 System.out.println("before transaction, table is:"); 144 printTable(conn1, SQL_REQUEST1); 145 printTable(conn2, SQL_REQUEST2); 146 147 try { 148 System.out.println("begin a transaction"); 149 utx.begin(); 150 151 System.out.println("update the table"); 152 updateTable(conn1, SQL_QUERY1, newValue); 153 updateTable(conn2, SQL_QUERY2, newValue); 154 155 if (completion.equals("commit")) { 156 System.out.println("*commit* the transaction"); 157 utx.commit(); 158 } else { 159 System.out.println("*rollback* the transaction"); 160 utx.rollback(); 161 } 162 } catch (Exception e) { 163 System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 164 System.out.println("Exception message :" + e.getMessage()); 165 e.printStackTrace(); 166 System.exit(1); 167 } 168 169 utx = null; 170 171 System.out.println("after transaction, table is:"); 172 printTable(conn1, SQL_REQUEST1); 173 printTable(conn2, SQL_REQUEST2); 174 175 try { 176 System.out.println("close connection"); 177 conn1.close(); 178 conn2.close(); 179 180 } catch (Exception e) { 181 e.printStackTrace(); 182 } finally { 183 conn1 = null; 184 conn2 = null; 185 } 186 187 System.out.println("stop server"); 188 ddbHelper.stop(); 189 190 System.out.println("JDBC example is ok.\n"); 191 System.exit(0); 192 } 193 } 194 | Popular Tags |