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