1 55 package org.lateralnz.simpletrans; 56 57 import java.io.Serializable ; 58 import java.sql.*; 59 import java.util.Map ; 60 61 import javax.transaction.xa.XAResource ; 62 import javax.transaction.xa.XAException ; 63 64 import org.apache.log4j.Logger; 65 66 import org.lateralnz.common.util.DAOUtils; 67 68 75 public class TransConnection implements Connection, XAResource , Serializable { 76 private static final Logger log = Logger.getLogger(TransConnection.class.getName()); 77 78 private String name; 79 private Connection conn; 80 private boolean close = false; 81 private boolean inTrans = false; 82 83 private long tempTime; 84 private java.util.ArrayList statements = null; 85 86 89 public TransConnection(String name, Connection conn, boolean inTrans) throws SQLException { 90 this.name = name; 91 this.conn = conn; 92 this.inTrans = inTrans; 93 this.conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 94 95 107 statements = new java.util.ArrayList (); 108 if (log.isDebugEnabled()) { 109 tempTime = System.currentTimeMillis(); 110 log.debug("created new transaction connection for jdbc conn " + conn); 111 } 112 } 113 114 117 public String getName() { 118 return name; 119 } 120 121 124 public void clearWarnings() throws SQLException { 125 conn.clearWarnings(); 126 } 127 128 133 public void close() throws SQLException { 134 if (!inTrans) { 135 doClose(); 136 } 137 else { 138 close = true; 139 } 140 } 141 142 145 public boolean isCloseFlagged() { 146 return close; 147 } 148 149 152 protected void doClose() throws SQLException { 153 if (log.isDebugEnabled()) { 154 log.debug("closing connection " + conn); 155 tempTime = System.currentTimeMillis() - tempTime; 156 if (tempTime > 15000) { 157 log.debug("time between opening and closing conn " + conn + " was " + tempTime + " after performing " + statements.toString()); 158 } 159 } 160 conn.close(); 161 } 162 163 168 public void commit() throws SQLException { 169 if (!inTrans) { 170 doCommit(); 171 } 172 } 173 174 177 public void doCommit() throws SQLException { 178 conn.commit(); 179 } 180 181 184 public Statement createStatement() throws SQLException { 185 return conn.createStatement(); 186 } 187 188 191 public Statement createStatement(int param, int param1) throws SQLException { 192 return conn.createStatement(param, param1); 193 } 194 195 198 public Statement createStatement(int param, int param1, int param2) throws SQLException { 199 return conn.createStatement(param, param1, param2); 200 } 201 202 205 public boolean getAutoCommit() throws SQLException { 206 return conn.getAutoCommit(); 207 } 208 209 212 public String getCatalog() throws SQLException { 213 return conn.getCatalog(); 214 } 215 216 219 public int getHoldability() throws SQLException { 220 return conn.getHoldability(); 221 } 222 223 226 public DatabaseMetaData getMetaData() throws SQLException { 227 return conn.getMetaData(); 228 } 229 230 233 public int getTransactionIsolation() throws SQLException { 234 return conn.getTransactionIsolation(); 235 } 236 237 240 public Map getTypeMap() throws SQLException { 241 return conn.getTypeMap(); 242 } 243 244 247 public SQLWarning getWarnings() throws SQLException { 248 return conn.getWarnings(); 249 } 250 251 254 public boolean isClosed() throws SQLException { 255 return conn.isClosed(); 256 } 257 258 261 public boolean isReadOnly() throws SQLException { 262 return conn.isReadOnly(); 263 } 264 265 268 public String nativeSQL(String str) throws SQLException { 269 return conn.nativeSQL(str); 270 } 271 272 275 public CallableStatement prepareCall(String str) throws SQLException { 276 return conn.prepareCall(str); 277 } 278 279 282 public CallableStatement prepareCall(String str, int param, int param2) throws SQLException { 283 return conn.prepareCall(str, param, param2); 284 } 285 286 289 public CallableStatement prepareCall(String str, int param, int param2, int param3) throws SQLException { 290 return conn.prepareCall(str, param, param2, param3); 291 } 292 293 296 public PreparedStatement prepareStatement(String str) throws SQLException { 297 statements.add(str); 298 return conn.prepareStatement(str); 299 } 300 301 304 public PreparedStatement prepareStatement(String str, int param) throws SQLException { 305 return conn.prepareStatement(str, param); 306 } 307 308 311 public PreparedStatement prepareStatement(String str, int[] values) throws SQLException { 312 return conn.prepareStatement(str, values); 313 } 314 315 318 public PreparedStatement prepareStatement(String str, String [] str1) throws SQLException { 319 return conn.prepareStatement(str, str1); 320 } 321 322 325 public PreparedStatement prepareStatement(String str, int param, int param2) throws SQLException { 326 return conn.prepareStatement(str, param, param2); 327 } 328 329 332 public PreparedStatement prepareStatement(String str, int param, int param2, int param3) throws SQLException { 333 return conn.prepareStatement(str, param, param2, param3); 334 } 335 336 339 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 340 conn.releaseSavepoint(savepoint); 341 } 342 343 348 public void rollback() throws SQLException { 349 if (!inTrans) { 350 doRollback(null); 351 } 352 } 353 354 359 public void rollback(Savepoint savepoint) throws SQLException { 360 if (!inTrans) { 361 doRollback(savepoint); 362 } 363 } 364 365 369 public void doRollback(Savepoint savepoint) throws SQLException { 370 if (savepoint == null) { 371 conn.rollback(); 372 } 373 else { 374 conn.rollback(savepoint); 375 } 376 } 377 378 381 public void setAutoCommit(boolean param) throws SQLException { 382 conn.setAutoCommit(param); 383 } 384 385 388 public void setCatalog(String str) throws SQLException { 389 conn.setCatalog(str); 390 } 391 392 395 public void setHoldability(int param) throws SQLException { 396 conn.setHoldability(param); 397 } 398 399 402 public void setReadOnly(boolean param) throws SQLException { 403 conn.setReadOnly(param); 404 } 405 406 409 public Savepoint setSavepoint() throws SQLException { 410 return conn.setSavepoint(); 411 } 412 413 416 public Savepoint setSavepoint(String str) throws SQLException { 417 return conn.setSavepoint(str); 418 } 419 420 423 public void setTransactionIsolation(int param) throws SQLException { 424 conn.setTransactionIsolation(param); 425 } 426 427 430 public void setTypeMap(Map map) throws SQLException { 431 conn.setTypeMap(map); 432 } 433 434 435 436 437 public void commit(javax.transaction.xa.Xid xid, boolean param) throws XAException { 438 throw new UnsupportedOperationException (); 439 } 440 441 public void end(javax.transaction.xa.Xid xid, int param) throws XAException { 442 throw new UnsupportedOperationException (); 443 } 444 445 public void forget(javax.transaction.xa.Xid xid) throws XAException { 446 throw new UnsupportedOperationException (); 447 } 448 449 public int getTransactionTimeout() throws XAException { 450 throw new UnsupportedOperationException (); 451 } 452 453 public boolean isSameRM(javax.transaction.xa.XAResource xAResource) throws XAException { 454 throw new UnsupportedOperationException (); 455 } 456 457 public int prepare(javax.transaction.xa.Xid xid) throws XAException { 458 return XAResource.XA_OK; 459 } 460 461 public javax.transaction.xa.Xid [] recover(int param) throws XAException { 462 throw new UnsupportedOperationException (); 463 } 464 465 public void rollback(javax.transaction.xa.Xid xid) throws XAException { 466 try { 467 this.rollback(); 468 } 469 catch (SQLException se) { 470 throw new XAException (se.getMessage()); 471 } 472 } 473 474 public boolean setTransactionTimeout(int param) throws XAException { 475 throw new UnsupportedOperationException (); 476 } 477 478 public void start(javax.transaction.xa.Xid xid, int param) throws XAException { 479 throw new UnsupportedOperationException (); 480 } 481 482 } | Popular Tags |