1 2 import org.enhydra.jdbc.pool.StandardXAPoolDataSource; 3 import org.enhydra.jdbc.standard.StandardXADataSource; 4 import org.objectweb.jotm.Jotm; 5 import org.objectweb.transaction.jta.TMService; 6 7 import javax.naming.InitialContext ; 8 import javax.naming.Context ; 9 import javax.transaction.UserTransaction ; 10 import java.util.Properties ; 11 import java.sql.ResultSet ; 12 import java.sql.PreparedStatement ; 13 import java.sql.Connection ; 14 import java.sql.Statement ; 15 16 30 public class SimpleStatement { 31 private String SQL_REQUEST = "select id, foo from testdata"; 32 private String SQL_QUERY = "update testdata set foo = ? where id=1"; 33 private String USAGE = "usage: java SimpleStatement [commit|rollback] [number]"; 34 35 private Connection conn; 36 private TMService jotm; 37 38 private String login = null; 39 private String password = null; 40 private String url = null; 41 private String driver = null; 42 private String USER_TRANSACTION_JNDI_NAME = "UserTransaction"; 43 44 public SimpleStatement(String [] args) throws Exception { 45 if (args.length != 2) { 46 System.out.println(USAGE); 47 System.exit(1); 48 } 49 50 Properties prop = new Properties (); 52 try { 53 prop.load(ClassLoader.getSystemResourceAsStream("spy.properties")); 54 } catch (Exception e) { 55 System.err.println("problem to load properties."); 56 e.printStackTrace(); 57 System.exit(1); 58 } 59 60 login = prop.getProperty("login"); 61 password = prop.getProperty("password"); 62 url = prop.getProperty("url"); 63 driver = prop.getProperty("driver"); 64 65 String completion = args[0]; 67 68 int newValue = 0; 70 try { 71 newValue = Integer.parseInt(args[1]); 72 } catch (NumberFormatException e) { 73 System.err.println(USAGE); 74 System.err.println("[number] has to be an integer\n"); 75 System.exit(1); 76 } 77 78 try { 80 jotm = new Jotm(true, false); 82 InitialContext ictx = new InitialContext (); 83 ictx.rebind(USER_TRANSACTION_JNDI_NAME, jotm.getUserTransaction()); 84 } catch (Exception e) { 85 System.err.println("JOTM problem."); 86 e.printStackTrace(); 87 System.exit(1); 88 } 89 90 UserTransaction utx = null; 92 try { 93 System.out.println("create initial context"); 94 Context ictx = new InitialContext (); 95 System.out.println("lookup UserTransaction at : " + USER_TRANSACTION_JNDI_NAME); 96 utx = (UserTransaction ) ictx.lookup(USER_TRANSACTION_JNDI_NAME); 97 } catch (Exception e) { 98 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 99 System.err.println("Exception message :" + e.getMessage()); 100 e.printStackTrace(); 101 System.exit(1); 102 } 103 104 StandardXAPoolDataSource spds = new StandardXAPoolDataSource(4); 106 spds.setMaxSize(15); 107 spds.setMinSize(13); 108 spds.setUser(login); 109 spds.setPassword(password); 110 111 StandardXADataSource xads = new StandardXADataSource(); 113 try { 114 xads.setDriverName(driver); 115 xads.setUrl(url); 116 xads.setUser(login); 117 xads.setPassword(password); 118 } catch (Exception e) { 119 e.printStackTrace(); 120 System.exit(1); 121 } 122 spds.setTransactionManager(jotm.getTransactionManager()); 123 spds.setDataSource(xads); 125 126 try { 127 System.out.println("** begin a transaction **"); 128 utx.begin(); 129 130 conn = spds.getConnection(login, password); 131 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 132 pstmt.setInt(1, newValue); 133 pstmt.executeUpdate(); 134 System.out.println("dump, after the first update:"); 135 printTable(); 136 pstmt.close(); 137 conn.close(); 138 139 conn = spds.getConnection(login, password); 140 PreparedStatement pstmt2 = conn.prepareStatement(SQL_QUERY); 141 pstmt2.setInt(1, newValue+2); 142 pstmt2.executeUpdate(); 143 System.out.println("dump, after the second update:"); 144 printTable(); 145 pstmt2.close(); 146 conn.close(); 147 148 if (completion.equals("commit")) { 149 System.out.println("** commit ** the transaction"); 150 utx.commit(); 151 } else { 152 System.out.println("** rollback ** the transaction"); 153 utx.rollback(); 154 } 155 } catch (Exception e) { 156 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 157 System.err.println("Exception message :" + e.getMessage()); 158 e.printStackTrace(); 159 System.exit(1); 160 } 161 162 System.out.println("dump, after work:"); 163 conn = spds.getConnection(login, password); 164 printTable(); 165 conn.close(); 166 stop(); 167 } 168 169 public void printTable() { 170 try { 171 Statement st = conn.createStatement(); 172 ResultSet rset = st.executeQuery(SQL_REQUEST); 173 int numcols = rset.getMetaData().getColumnCount(); 174 for (int i = 1; i <= numcols; i++) { 175 System.out.print("\t" + rset.getMetaData().getColumnName(i)); 176 } 177 System.out.println(); 178 while (rset.next()) { 179 for (int i = 1; i <= numcols; i++) { 180 System.out.print("\t" + rset.getString(i)); 181 } 182 System.out.println(""); 183 } 184 st.close(); 185 } catch (Exception e) { 186 e.printStackTrace(); 187 } 188 } 189 190 public void stop() { 191 try { 192 InitialContext ictx = new InitialContext (); 193 ictx.unbind(USER_TRANSACTION_JNDI_NAME); 194 } catch (Exception e) { 195 e.printStackTrace(); 196 } 197 jotm.stop(); 198 jotm = null; 199 } 200 201 static public void main(String [] argv) throws Exception { 202 SimpleStatement spdse = new SimpleStatement(argv); 203 System.exit(1); 204 } 205 } 206 | Popular Tags |