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