1 import java.sql.Connection ; 2 import java.sql.PreparedStatement ; 3 import java.sql.ResultSet ; 4 import java.sql.SQLException ; 5 import java.sql.Timestamp ; 6 7 import java.util.Properties ; 8 9 import javax.naming.Context ; 10 import javax.naming.InitialContext ; 11 import javax.sql.DataSource ; 12 import javax.transaction.RollbackException ; 13 import javax.transaction.Status ; 14 import javax.transaction.Transaction ; 15 import javax.transaction.TransactionManager ; 16 import javax.transaction.UserTransaction ; 17 18 import org.enhydra.jdbc.pool.StandardXAPoolDataSource; 19 import org.enhydra.jdbc.standard.StandardXADataSource; 20 import org.objectweb.jotm.Jotm; 21 import org.objectweb.transaction.jta.TMService; 22 23 31 public class Connect implements Runnable { 32 33 String client; 34 int nb; 35 UserTransaction ut; 36 String productlist; 37 private static TMService jotm; 38 private static TransactionManager tm; 39 private Transaction t; 40 private static StandardXAPoolDataSource spds1; 41 private static StandardXAPoolDataSource spds2; 42 43 46 public Connect(String s, String mydo, int index) { 47 super(); 48 49 client = mydo; 50 nb = index; 51 productlist = s; 52 53 } 54 55 private static synchronized void init() { 56 String login = null; 57 String password = null; 58 String url = null; 59 String driver = null; 60 61 try { 63 if (jotm == null) 64 jotm = new Jotm(true, false); 65 if (tm == null) 66 tm = jotm.getTransactionManager(); 67 if (tm == null) { 68 System.out.println("moniteur transactionel non initialisé"); 69 return; 70 } 71 72 73 74 Properties prop = new Properties (); 76 try { 77 prop.load(ClassLoader.getSystemResourceAsStream("spy.properties")); 78 } catch (Exception e) { 79 System.err.println("problem to load properties."); 80 e.printStackTrace(); 81 System.exit(1); 82 } 83 84 login = prop.getProperty("login"); 85 password = prop.getProperty("password"); 86 url = prop.getProperty("url"); 87 driver = prop.getProperty("driver"); 88 89 90 91 92 if (spds1 == null) { 94 95 spds1 = new StandardXAPoolDataSource(4); 96 spds1.setMaxSize(15); 97 spds1.setMinSize(13); 98 spds1.setUser(login); 99 spds1.setPassword(password); 100 StandardXADataSource xds1 = new StandardXADataSource(); 101 102 xds1.setDriverName(driver); 103 xds1.setUrl(url); 104 xds1.setUser(login); 105 xds1.setPassword(password); 106 spds1.setTransactionManager(tm); 107 spds1.setDataSource(xds1); 108 } 109 if (spds2 == null) { 110 spds2 = new StandardXAPoolDataSource(4); 111 spds2.setMaxSize(15); 112 spds2.setMinSize(13); 113 spds2.setUser(login); 114 spds2.setPassword(password); 115 StandardXADataSource xds2 = new StandardXADataSource(); 116 117 xds2.setDriverName(driver); 118 xds2.setUrl(url); 119 xds2.setUser(login); 120 xds2.setPassword(password); 121 spds2.setTransactionManager(tm); 122 spds2.setDataSource(xds2); 123 } 124 125 } catch (Exception e) { 126 System.out.println("Problem jotm/xapool"); 127 System.exit(0); 128 } 129 } 130 131 public void run() { 132 int res = 0; 133 ut = jotm.getUserTransaction(); 135 DataSource hds1 = null; 136 PreparedStatement ps1 = null; 137 Connection conn1 = null; 138 PreparedStatement ps2b = null; 139 Connection conn2b = null; 140 DataSource hds2 = null; 141 PreparedStatement ps2 = null; 142 Connection conn2 = null; 143 for (int tr = 0; tr < productlist.length(); tr++) { 144 try { 145 146 if (ut == null) 147 throw new Exception ("ut is null"); 148 ut.begin(); 149 150 int size = 9; 152 char[] cs = new char[size]; 153 char c = productlist.charAt(tr); 154 155 for (int l = 0; l < size; l++) 156 cs[l] = c; 157 String obj = String.valueOf(cs); 158 String objs = Integer.toString(++nb); 159 160 Timestamp dtcr = null; 161 162 try { 163 System.out.println("insert BASE1 "); 164 hds1 = (DataSource ) spds1; 165 conn1 = hds1.getConnection(); 166 167 dtcr = new Timestamp (System.currentTimeMillis()); 168 ps1 = 169 conn1.prepareStatement( 170 "insert into hproduct (name,createdate,updatedate) VALUES (?,?,?)"); 171 172 ps1.setString(1, obj); 173 ps1.setTimestamp(2, dtcr); 174 ps1.setTimestamp(3, dtcr); 175 res = ps1.executeUpdate(); 176 177 } catch (Exception e) { 178 System.out.println( 179 "pb insert BASE1: " 180 + e.getClass() 181 + ": " 182 + e.getMessage()); 183 ut.setRollbackOnly(); 184 185 } finally { 186 try { 187 if (ps1 != null) 188 ps1.close(); 189 if (conn1 != null) 190 conn1.close(); 191 conn1 = null; 192 } catch (SQLException sqle) { 193 System.out.println( 194 "close pb BASE1: " 195 + sqle.getClass() 196 + ": " 197 + sqle.getMessage()); 198 } 199 200 } 201 203 try { 204 System.out.println("insert BASE2 "); 205 hds2 = (DataSource ) spds2; 206 conn2b = hds2.getConnection(); 207 208 dtcr = new Timestamp (System.currentTimeMillis()); 209 ps2b = 210 conn2b.prepareStatement( 211 "insert into horder (ordernum,customer,createdate,updatedate) VALUES (?,?,?,?)"); 212 ps2b.setString(1, objs); 213 ps2b.setString(2, client); 214 ps2b.setTimestamp(3, dtcr); 215 ps2b.setTimestamp(4, dtcr); 216 res = ps2b.executeUpdate(); 217 218 } catch (Exception e) { 219 System.out.println( 220 "pb insert BASE2: " 221 + e.getClass() 222 + ": " 223 + e.getMessage()); 224 ut.setRollbackOnly(); 225 } finally { 226 try { 227 if (ps2b != null) 228 ps2b.close(); 229 if (conn2b != null) 230 conn2b.close(); 231 conn2b = null; 232 } catch (SQLException sqle) { 233 System.out.println( 234 "close pb BASE2: " 235 + sqle.getClass() 236 + ": " 237 + sqle.getMessage()); 238 } 239 } 240 241 int stat = ut.getStatus(); 242 if (stat == Status.STATUS_ACTIVE) { 243 System.out.println( 244 "##### ut commit " + obj + " " + objs + " " + client); 245 ut.commit(); 246 } else { 247 System.out.println( 248 "##### ut rollback " + obj + " " + objs + " " + client); 249 ut.rollback(); 250 } 251 } catch (Exception e) { 252 System.out.println( 253 "UT termination: " + e.getClass() + ": " + e.getMessage()); 254 try { 255 if (!(e instanceof RollbackException )) { 256 System.out.println("ut rollback..."); 257 ut.rollback(); 258 } 259 } catch (Exception e1) { 260 System.out.println( 261 "rollback: " + e1.getClass() + ": " + e1.getMessage()); 262 } 263 264 } 265 266 } 267 268 } 269 public static void main(String [] args) { 270 271 init(); 272 273 Connect co1 = 274 new Connect("abcdefghijklmnopqrstuvwxyz", "leclerc", 1000); 275 276 Connect co2 = 277 new Connect("ABCDEFGHIJKLMNOPQRSTUVWXYa", "carrefour", 2000); 278 279 Thread t1 = new Thread (co1); 280 Thread t2 = new Thread (co2); 281 282 t1.start(); 283 t2.start(); 284 try { 285 t1.join(); 286 t2.join(); 287 288 } catch (InterruptedException ie) { 289 290 } finally { 291 System.exit(0); 292 } 293 294 } 295 } 296 | Popular Tags |