1 22 package org.enhydra.jdbc.standard; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 import java.sql.CallableStatement ; 28 import java.util.Hashtable ; 29 import javax.transaction.Transaction ; 30 import javax.transaction.TransactionManager ; 31 import javax.transaction.RollbackException ; 32 import javax.transaction.SystemException ; 33 34 public class StandardXAConnectionHandle extends StandardConnectionHandle { 35 36 boolean resetTxonResume =false; 37 boolean globalTransaction; public TransactionManager transactionManager; 39 public Transaction tx; 40 public StandardXAConnection xacon; 41 public boolean thisAutoCommit = true; 42 43 46 public StandardXAConnectionHandle( 47 StandardXAConnection pooledCon, 48 Hashtable preparedStatementCache, 49 int preparedStmtCacheSize, 50 TransactionManager tm) { 51 super(pooledCon, preparedStatementCache, preparedStmtCacheSize); 52 xacon = pooledCon; 54 transactionManager = tm; 55 log = pooledCon.dataSource.log; 56 57 } 60 61 public void setTransactionManager(TransactionManager tm) { 62 this.transactionManager = tm; 63 } 64 65 synchronized public void close() throws SQLException { 66 Transaction ttx = tx; 67 super.close(); 70 log.debug("StandardXAConnectionHandle:close"); 71 log.debug( 72 "StandardXAConnectionHandle:close globalTransaction='" 73 + globalTransaction 74 + "' con.getAutoCommit='" 75 + con.getAutoCommit() 76 + "' ttx='" 77 + ttx 78 + "'"); 79 80 if ((!con.getAutoCommit()) && (ttx == null)) { 81 log.debug( 82 "StandardXAConnectionHandle:close rollback the connection"); 83 con.rollback(); 84 con.setAutoCommit(thisAutoCommit); 85 } else 86 log.debug("StandardXAConnectionHandle:close do nothing else"); 87 isReallyUsed = false; 88 log.debug( 89 "StandardXAConnectionHandle:close AFTER globalTransaction='" 90 + globalTransaction 91 + "' con.getAutoCommit='" 92 + con.getAutoCommit() 93 + "' ttx='" 94 + ttx 95 + "'"); 96 } 97 98 102 void setGlobalTransaction(boolean setting) throws SQLException { 103 log.debug( 104 "StandardXAConnectionHandle:setGlobalTransaction gTransaction='" 105 + setting 106 + "'"); 107 globalTransaction = setting; con = pooledCon.getPhysicalConnection(); if (con == null) 110 log.warn( 111 "StandardXAConnectionHandle:setGlobalTransaction con is null before setupPreparedStatementCache"); 112 else 113 log.debug( 114 "StandardXAConnectionHandle:setGlobalTransaction con is *NOT* null before setupPreparedStatementCache"); 115 if(!isClosed()) 117 super.setAutoCommit(!setting); 118 } 120 121 public void setAutoCommit(boolean autoCommit) throws SQLException { 122 if (globalTransaction) throw new SQLException ("StandardXAConnectionHandle:setAutoCommit This connection is part of a global transaction"); 124 super.setAutoCommit(autoCommit); 125 } 126 127 public void commit() throws SQLException { 128 if (globalTransaction) throw new SQLException ("StandardXAConnectionHandle:commit:This connection is part of a global transaction"); 130 super.commit(); 131 } 133 134 public void rollback() throws SQLException { 135 if (globalTransaction) throw new SQLException ("StandardXAConnectionHandle:rollback:This connection is part of a global transaction"); 137 super.rollback(); 138 } 140 141 synchronized PreparedStatement checkPreparedCache( 142 String sql, 143 int type, 144 int concurrency, 145 int holdability, 146 Object lookupKey) 147 throws SQLException { 148 PreparedStatement ret = null; if (preparedStatementCache != null) { 153 Object obj = preparedStatementCache.get(lookupKey); 154 if (obj != null) { log.debug( 157 "StandardXAConnectionHandle:checkPreparedCache object is found"); 158 ret = (PreparedStatement ) obj; try { 160 ret.clearParameters(); } catch (SQLException e) { 162 ret = createPreparedStatement(sql, type, concurrency, holdability); 164 } 166 preparedStatementCache.remove(lookupKey); 167 inUse.put(lookupKey, ret); 169 } else { log.debug( 172 "StandardXAConnectionHandle:checkPreparedCache object is *NOT* found"); 173 ret = createPreparedStatement(sql, type, concurrency, holdability); 174 inUse.put(lookupKey, ret); 176 } 178 } else { 179 log.debug( 180 "StandardXAConnectionHandle:checkPreparedCache object the cache is out"); 181 ret = createPreparedStatement(sql, type, concurrency, holdability); 182 } 184 log.debug( 188 "StandardXAConnectionHandle:checkPreparedCache pstmt='" 189 + ret.toString() 190 + "'"); 191 return ret; 192 } 193 194 195 196 synchronized PreparedStatement checkPreparedCache( 197 String sql, 198 int autogeneratedkeys, 199 Object lookupKey) 200 throws SQLException { 201 PreparedStatement ret = null; if (preparedStatementCache != null) { 206 Object obj = preparedStatementCache.get(lookupKey); 207 if (obj != null) { log.debug( 210 "StandardXAConnectionHandle:checkPreparedCache object is found"); 211 ret = (PreparedStatement ) obj; try { 213 ret.clearParameters(); } catch (SQLException e) { 215 ret = createPreparedStatement(sql, autogeneratedkeys); 217 } 219 preparedStatementCache.remove(lookupKey); 220 inUse.put(lookupKey, ret); 222 } else { log.debug( 225 "StandardXAConnectionHandle:checkPreparedCache object is *NOT* found"); 226 ret = createPreparedStatement(sql, autogeneratedkeys); 227 inUse.put(lookupKey, ret); 229 } 231 } else { 232 log.debug( 233 "StandardXAConnectionHandle:checkPreparedCache object the cache is out"); 234 ret = createPreparedStatement(sql, autogeneratedkeys); 235 } 237 log.debug( 241 "StandardXAConnectionHandle:checkPreparedCache pstmt='" 242 + ret.toString() 243 + "'"); 244 return ret; 245 } 246 247 248 249 253 public PreparedStatement prepareStatement(String sql) throws SQLException { 254 return prepareStatement(sql, 0, 0, 0); 255 } 256 257 public PreparedStatement prepareStatement( 258 String sql, 259 int resultSetType, 260 int resultSetConcurrency) 261 throws SQLException { 262 return prepareStatement(sql, resultSetType, resultSetConcurrency, 0); 263 } 264 265 269 public PreparedStatement prepareStatement( 270 String sql, 271 int resultSetType, 272 int resultSetConcurrency, 273 int resultSetHoldability) 274 throws SQLException { 275 if (tx == null) { 276 log.debug("StandardXAConnectionHandle:prepareStatement tx==null"); 277 try { 278 try { 279 Transaction ntx = this.getTransaction(); 280 if (ntx != null) { 281 log.debug( 282 "StandardXAConnectionHandle:prepareStatement (found a transaction)"); 283 tx = ntx; 284 xacon.thisAutoCommit = this.getAutoCommit(); 285 if (this.getAutoCommit()) { 286 this.setAutoCommit(false); 287 } 288 try { 289 tx.enlistResource(xacon.getXAResource()); 290 } catch (RollbackException n) { 292 log.debug( 293 "StandardXAConnectionHandle:prepareStatemnet enlistResource exception : " 294 + n.toString()); 295 } 296 } else { 297 log.debug( 298 "StandardXAConnectionHandle:prepareStatement (no transaction found)"); 299 } 300 } catch (SystemException n) { 301 n.printStackTrace(); 302 throw new SQLException ( 303 "StandardXAConnectionHandle:prepareStatement getTransaction exception: " 304 + n.toString()); 305 } 306 } catch (NullPointerException n) { 307 n.printStackTrace(); 309 throw new SQLException ("StandardXAConnectionHandle:prepareStatement should not be used outside an EJBServer"); 310 } 311 } else 312 log.debug("StandardXAConnectionHandle:prepareStatement tx!=null"); 313 314 319 isReallyUsed = true; 320 return new StandardXAPreparedStatement( 321 this, 322 sql, 323 resultSetType, 324 resultSetConcurrency, 325 resultSetHoldability); 326 } 327 328 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) 329 throws SQLException { 330 if (tx == null) { 331 log.debug("StandardXAConnectionHandle:prepareStatement tx==null"); 332 try { 333 try { 334 Transaction ntx = this.getTransaction(); 335 if (ntx != null) { 336 log.debug( 337 "StandardXAConnectionHandle:prepareStatement (found a transaction)"); 338 tx = ntx; 339 xacon.thisAutoCommit = this.getAutoCommit(); 340 if (this.getAutoCommit()) { 341 this.setAutoCommit(false); 342 } 343 try { 344 tx.enlistResource(xacon.getXAResource()); 345 } catch (RollbackException n) { 347 log.debug( 348 "StandardXAConnectionHandle:prepareStatemnet enlistResource exception : " 349 + n.toString()); 350 } 351 } else { 352 log.debug( 353 "StandardXAConnectionHandle:prepareStatement (no transaction found)"); 354 } 355 } catch (SystemException n) { 356 n.printStackTrace(); 357 throw new SQLException ( 358 "StandardXAConnectionHandle:prepareStatement getTransaction exception: " 359 + n.toString()); 360 } 361 } catch (NullPointerException n) { 362 n.printStackTrace(); 364 throw new SQLException ("StandardXAConnectionHandle:prepareStatement should not be used outside an EJBServer"); 365 } 366 } else 367 log.debug("StandardXAConnectionHandle:prepareStatement tx!=null"); 368 369 isReallyUsed = true; 370 return new StandardXAPreparedStatement( 371 this, 372 sql, 373 autoGeneratedKeys); 374 } 375 376 379 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { 380 throw new UnsupportedOperationException (); 381 } 382 383 386 public PreparedStatement prepareStatement(String sql, String [] columnNames) throws SQLException { 387 throw new UnsupportedOperationException (); 388 } 389 390 391 394 public CallableStatement prepareCall( 395 String sql, 396 int resultSetType, 397 int resultSetConcurrency) 398 throws SQLException { 399 return new StandardXACallableStatement( 400 this, 401 sql, 402 resultSetType, 403 resultSetConcurrency, 0); 404 } 405 406 409 public CallableStatement prepareCall(String sql) throws SQLException { 410 return new StandardXACallableStatement(this, sql, 0, 0, 0); 411 } 412 413 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) 414 throws SQLException { 415 return new StandardXACallableStatement( 416 this, 417 sql, 418 resultSetType, 419 resultSetConcurrency, 420 resultSetHoldability); 421 } 422 423 424 public Statement createStatement() throws SQLException { 425 return createStatement(0,0,0); 426 } 427 428 public Statement createStatement( 429 int resultSetType, 430 int resultSetConcurrency) 431 throws SQLException { 432 return createStatement(resultSetType, resultSetConcurrency, 0); 433 } 434 435 public Statement createStatement( 436 int resultSetType, 437 int resultSetConcurrency, 438 int resultSetHoldability) 439 throws SQLException { 440 441 if (tx == null) { 442 log.debug("StandardXAConnectionHandle:createStatement tx==null"); 443 try { 444 try { 445 Transaction ntx = this.getTransaction(); 446 if (ntx != null) { 447 log.debug( 448 "StandardXAConnectionHandle:createStatement (found a transaction)"); 449 tx = ntx; 450 xacon.thisAutoCommit = this.getAutoCommit(); 451 if (this.getAutoCommit()) { 452 this.setAutoCommit(false); 453 } 454 try { 455 tx.enlistResource(xacon.getXAResource()); 456 } catch (RollbackException n) { 458 log.debug( 459 "StandardXAConnectionHandle:createStatement enlistResource exception: " 460 + n.toString()); 461 } 462 } else { 463 log.debug( 464 "StandardXAConnectionHandle:createStatement (no transaction found)"); 465 } 466 467 } catch (SystemException n) { 468 throw new SQLException ( 469 "StandardXAConnectionHandle:createStatement getTransaction exception: " 470 + n.toString()); 471 } 472 } catch (NullPointerException n) { 473 throw new SQLException ( 475 "StandardXAConnectionHandle:createStatement should not be used outside an EJBServer: " 476 + n.toString()); 477 } 478 } 479 isReallyUsed = true; 480 return new StandardXAStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability); 481 } 482 483 private Transaction getTransaction() throws SystemException { 484 Transaction ntx = null; 485 if (transactionManager != null) { 486 ntx = transactionManager.getTransaction(); 487 } else { 488 log.debug( 489 "StandardXAConnectionHandle:getTransaction (null transaction manager)"); 490 } 491 492 return ntx; 493 } 494 495 public String toString() { 496 StringBuffer sb = new StringBuffer (); 497 sb.append("StandardXAConnectionHandle:\n"); 498 sb.append(" global transaction =<"+this.globalTransaction+ ">\n"); 499 sb.append(" is really used =<"+this.isReallyUsed+ ">\n"); 500 sb.append(" this autoCommit =<"+this.thisAutoCommit+ ">\n"); 501 sb.append(" in use size =<"+this.inUse.size()+ ">\n"); 502 sb.append(" master prepared stmt cache size =<"+this.masterPrepStmtCache.size()+ ">\n"); 503 sb.append(" transaction =<"+this.tx+ ">\n"); 504 sb.append(" connection =<"+this.con.toString()+ ">\n"); 505 506 return sb.toString(); 507 } 508 } 509 | Popular Tags |