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 15 28 public class SaveAutoCommit { 29 private String SQL_REQUEST = "select id, foo from testdata"; 30 private String SQL_QUERY = "update testdata set foo = ? where id=1"; 31 private String USAGE = "usage: java SaveAutoCommit [number] [autocommit]"; 32 33 private Connection conn; 34 private TMService jotm; 35 36 private String login = null; 37 private String password = null; 38 private String url = null; 39 private String driver = null; 40 private String USER_TRANSACTION_JNDI_NAME = "UserTransaction"; 41 private PreparedStatement STMT = null; 42 43 public SaveAutoCommit(String [] args) throws Exception { 44 if (args.length != 2) { 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 try { 76 jotm = new Jotm(true, false); 78 InitialContext ictx = new InitialContext (); 79 ictx.rebind(USER_TRANSACTION_JNDI_NAME, jotm.getUserTransaction()); 80 } catch (Exception e) { 81 System.err.println("JOTM problem"); 82 e.printStackTrace(); 83 System.exit(1); 84 } 85 86 UserTransaction utx = null; 88 try { 89 System.out.println("create initial context"); 90 Context ictx = new InitialContext (); 91 System.out.println("lookup UserTransaction at : " + USER_TRANSACTION_JNDI_NAME); 92 utx = (UserTransaction ) ictx.lookup(USER_TRANSACTION_JNDI_NAME); 93 } catch (Exception e) { 94 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 95 System.err.println("Exception message :" + e.getMessage()); 96 e.printStackTrace(); 97 System.exit(1); 98 } 99 100 101 StandardXAPoolDataSource spds = new StandardXAPoolDataSource(4); 103 spds.setMaxSize(15); 104 spds.setMinSize(13); 105 spds.setUser(login); 106 spds.setPassword(password); 107 108 StandardXADataSource xads = new StandardXADataSource(); 110 try { 111 xads.setDriverName(driver); 112 xads.setUrl(url); 113 xads.setUser(login); 114 xads.setPassword(password); 115 xads.setTransactionManager(jotm.getTransactionManager()); 116 } catch (Exception e) { 117 e.printStackTrace(); 118 System.exit(1); 119 } 120 spds.setDataSource(xads); 122 123 try { 124 conn = spds.getConnection(login, password); 125 boolean autocom =(new Boolean (args[1].trim())).booleanValue(); 126 conn.setAutoCommit(autocom); 127 128 utx.begin(); 129 PreparedStatement pstmt = conn.prepareStatement(SQL_QUERY); 130 pstmt.setInt(1, newValue); 131 pstmt.executeUpdate(); 132 utx.commit(); 133 System.out.println("SetAutoCommit is '"+conn.getAutoCommit()+"'"+ 134 " and should be '"+autocom+"'"); 135 conn.close(); 136 137 } catch (Exception e) { 138 System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 139 System.out.println("Exception message :" + e.getMessage()); 140 e.printStackTrace(); 141 System.exit(1); 142 } 143 144 stop(); 145 } 146 147 public void printTable() { 148 try { 149 STMT = conn.prepareStatement(SQL_REQUEST); 150 ResultSet rset = STMT.executeQuery(SQL_REQUEST); 151 int numcols = rset.getMetaData().getColumnCount(); 152 for (int i = 1; i <= numcols; i++) { 153 System.out.print("\t" + rset.getMetaData().getColumnName(i)); 154 } 155 System.out.println(); 156 while (rset.next()) { 157 for (int i = 1; i <= numcols; i++) { 158 System.out.print("\t" + rset.getString(i)); 159 } 160 System.out.println(""); 161 } 162 } catch (Exception e) { 163 e.printStackTrace(); 164 } 165 } 166 167 public void stop() { 168 try { 169 InitialContext ictx = new InitialContext (); 170 ictx.unbind(USER_TRANSACTION_JNDI_NAME); 171 } catch (Exception e) { 172 e.printStackTrace(); 173 } 174 jotm.stop(); 175 jotm = null; 176 } 177 178 static public void main(String [] argv) throws Exception { 179 SaveAutoCommit spdse = new SaveAutoCommit(argv); 180 System.exit(1); 181 } 182 } 183 | Popular Tags |