1 64 65 package com.jcorporate.expresso.core.db; 66 67 import com.jcorporate.expresso.core.misc.StringUtil; 68 import org.apache.log4j.Logger; 69 70 71 80 public class DBTransaction { 81 82 85 public static final String DEFAULT_DB_CONTEXT_NAME = "default"; 86 87 90 private int transactionId = 0; 91 92 95 private int lastCommited = 0; 96 97 100 private String myDescription = "no description"; 101 102 105 private String dbName = DEFAULT_DB_CONTEXT_NAME; 106 107 110 private boolean isConnected = false; 111 112 115 private DBConnectionPool myPool; 116 117 120 private DBConnection myConnection; 121 122 125 private static final String THIS_CLASS = DBTransaction.class.getName() + "."; 126 127 130 private long createdTime = System.currentTimeMillis(); 131 132 135 private static Logger transLog = Logger.getLogger("com.jcorporate.expresso.core.db.TRANSACTION"); 136 137 142 private static Logger log = Logger.getLogger(DBTransaction.class); 143 144 153 public DBTransaction() throws DBException { 154 transactionSetup(DEFAULT_DB_CONTEXT_NAME); 155 } 156 157 158 167 public DBTransaction(DBConnectionPool pool) throws DBException { 168 myPool = pool; 169 dbName = myPool.getDataContext(); 170 } 171 172 173 182 public DBTransaction(String transactionDataContext) throws DBException { 183 transactionSetup(transactionDataContext); 184 } 185 186 187 193 private void transactionSetup(String transactionDataContext) throws DBException { 194 String myName = (THIS_CLASS + "transactionSetup()"); 195 try { 196 myPool = DBConnectionPool.getInstance(transactionDataContext); 197 dbName = transactionDataContext; 198 } catch (DBException e) { 199 myPool = null; 200 log.error(myName + "DBException : initialize pool for transaction failed!", e); 201 } 202 } 203 204 205 211 public void startTransaction() throws DBException { 212 startTransaction(true); 213 } 214 215 221 public void startTransaction(boolean immortal) throws DBException { 222 String myName = (THIS_CLASS + "startTransaction()"); 223 224 try { 225 if (!isConnected) { 226 if (myPool != null) { 227 myConnection = myPool.getConnection(getDataContext()); 228 myConnection.setImmortal(immortal); 229 myConnection.setAutoCommit(false); 230 isConnected = true; 231 setId(myConnection.getId()); 232 lastCommited = 0; 233 } else { 234 return; 235 } 236 } 237 lastCommited++; 238 if (transLog.isDebugEnabled()) { 239 transLog.debug(myName + " Transaction " + myConnection.getId() + " Starting:'" + 240 "' on db '" + getDataContext() + "'"); 241 } 242 } catch (DBException e) { 243 log.error(myName + "DBException : initialize transaction failed!", e); 244 if (myConnection != null) { 245 close(); 246 } 247 } 248 } 249 250 253 public void clear() throws DBException { 254 if (myConnection != null) { 255 rollback(); 256 } 257 } 258 259 260 267 public void commit() throws DBException { 268 String myName = (THIS_CLASS + "commit()"); 269 try { 270 if (!myConnection.isClosed()) { 272 myConnection.commit(); 273 if (log.isDebugEnabled()) { 274 log.debug(myName + " Transaction " + myConnection.getId() + " Commited:'" + 275 "' on db '" + getDataContext() + "'"); 276 } 277 close(); 278 } 279 283 } catch (DBException se) { 284 close(); 285 throw new DBException(myName + ":Could not commit (" + 286 myDescription + ")", se.getMessage()); 287 } 288 } 289 290 293 private void close() throws DBException { 294 String myName = (THIS_CLASS + "close()"); 295 myConnection.setImmortal(false); 296 if (log.isDebugEnabled()) { 297 log.debug(myName + " Transaction " + myConnection.getId() + " Closed:'" + 298 "' on db '" + getDataContext() + "'"); 299 } 300 myPool.release(myConnection); 301 myConnection = null; 302 myPool = null; 303 setId(0); 304 dbName = ""; 305 isConnected = false; 306 lastCommited = -1; 307 } 308 309 310 327 public DBConnection getTransactionConnection() { 328 if (myConnection == null) { 329 return null; 330 } 331 return myConnection; 332 } 333 334 341 public String getDescription() { 342 return myDescription; 343 } 344 345 352 public boolean isClosed() 353 throws DBException { 354 String myName = (THIS_CLASS + "isClosed()"); 355 356 try { 357 return myConnection.isClosed(); 358 } catch (DBException se) { 359 throw new DBException(myName + "isClosed()" + ":Cannot access connection to " + 360 "database via (driver or JNDI) '" + myConnection.getDBDriver() + 361 "' and URL '" + myConnection.getDBURL() + "' (" + 362 myDescription + ")", se.getMessage()); 363 } 364 } 365 366 367 374 public boolean isConnected() { 375 return isConnected; 376 } 377 378 379 386 public void rollback() throws DBException { 387 String myName = (THIS_CLASS + "rollback()"); 388 try { 389 if (!myConnection.isClosed()) { 390 myConnection.rollback(); 391 if (log.isDebugEnabled()) { 392 log.debug(myName + " Transaction " + myConnection.getId() + " Rollback:'" + 393 "' on db '" + getDataContext() + "'"); 394 } 395 close(); 396 } 397 } catch (DBException se) { 398 close(); 399 throw new DBException(myName + " Transaction" + ":Could not rollback" + " (" + 400 myDescription + ")", se.getMessage()); 401 } 402 } 403 404 405 413 public void setDescription(String newDescription) { 414 if (newDescription != null) { 415 myDescription = newDescription; 416 } 417 418 } 419 420 425 public void setDataContext(String newDBName) { 426 if (StringUtil.notNull(newDBName).length() == 0) { 427 dbName = DEFAULT_DB_CONTEXT_NAME; 428 } else { 429 dbName = newDBName; 430 } 431 } 432 433 438 public String getDataContext() { 439 return dbName; 440 } 441 442 453 public void release() throws DBException { 454 String myName = (THIS_CLASS + "release()"); 455 if (myConnection != null) { 456 myConnection.setImmortal(false); 457 if (log.isDebugEnabled()) { 458 log.debug(myName + " Transaction " + myConnection.getId() + " Released:'" + 459 "' on db '" + getDataContext() + "'"); 460 } 461 myPool.release(myConnection); 462 myConnection = null; 463 myPool = null; 464 } else { 465 log.warn(myName + "No parent connection pool defined for this DBConnection"); 466 } 467 468 } 469 470 473 public DBConnectionPool getPool() { 474 return myPool; 475 } 476 477 480 public void setPool(DBConnectionPool pool) { 481 myPool = pool; 482 } 483 484 491 public void setTransactionReadOnlyMode() throws DBException { 492 String myName = (THIS_CLASS + "setTransactionReadOnlyMode()"); 493 494 try { 495 myConnection.setTransactionCommittedMode(); 496 } catch (DBException se) { 497 throw new DBException(myName + "Transaction " + myDescription + "." + myConnection.getId() + " Transaction Committed Mode :'" + 498 "' on db '" + getDataContext() + "'" + se); 499 } 500 } 501 502 509 public void setTransactionDirtyMode() throws DBException { 510 String myName = (THIS_CLASS + "setTransactionDirtyMode()"); 511 512 try { 513 myConnection.setTransactionUncommittedMode(); 514 } catch (DBException se) { 515 throw new DBException(myName + "Transaction " + myDescription + "." + myConnection.getId() + " Transaction Uncommitted Mode :'" + 516 "' on db '" + getDataContext() + "'" + se); 517 } 518 } 519 520 527 public void setTransactionRestrictiveMode() throws DBException { 528 String myName = (THIS_CLASS + "setTransactionExclusiveMode()"); 529 530 try { 531 myConnection.setTransactionRepeatableMode(); 532 } catch (DBException se) { 533 throw new DBException(myName + "Transaction " + myDescription + "." + myConnection.getId() + " Transaction Repeatable Mode :'" + 534 "' on db '" + getDataContext() + "'" + se); 535 } 536 } 537 538 545 public void setTransactionExclusiveMode() throws DBException { 546 String myName = (THIS_CLASS + "setTransactionExclusiveMode()"); 547 548 try { 549 myConnection.setTransactionSerializableMode(); 550 } catch (DBException se) { 551 throw new DBException(myName + " Transaction " + myDescription + "." + myConnection.getId() + " Transaction Serialisable Mode :'" + 552 "' on db '" + getDataContext() + "'" + se); 553 } 554 } 555 556 564 public int getId() { 565 return transactionId; 566 } 567 568 573 private void setId(int newTransactionID) { 574 transactionId = newTransactionID; 575 } 576 577 585 public String getTransactionIdentifier() { 586 return myDescription + "." + Integer.toString(transactionId); 587 } 588 589 590 } 591 592 | Popular Tags |