001: package com.completex.objective.components.persistency.transact.impl;
002:
003: import com.completex.objective.components.persistency.OdalPersistencyException;
004: import com.completex.objective.components.persistency.transact.Transaction;
005: import com.completex.objective.components.persistency.transact.TransactionManager;
006: import com.completex.objective.components.OdalRuntimeException;
007:
008: import java.sql.SQLException;
009:
010: /**
011: * Transaction manager implementation that always joins current transaction if it exists and
012: * never commits or rolls back.
013: *
014: * @author Gennady Krizhevsky
015: */
016: public class JoiningNullTransactionManagerImpl implements
017: TransactionManager {
018:
019: private final TransactionManager coreTransactionManager;
020:
021: public JoiningNullTransactionManagerImpl(
022: TransactionManager coreTransactionManager) {
023: this .coreTransactionManager = coreTransactionManager;
024: }
025:
026: /**
027: * If transaction exists - returns new instance of JoiningNullTransactionImpl that is
028: * wrapper for "real" current transaction
029: *
030: * @return current transaction
031: * @throws OdalPersistencyException if no current transaction found
032: */
033: public Transaction begin() throws SQLException {
034: Transaction transaction = getCurrentCoreTransactionSafe();
035: return newTransaction(transaction);
036: }
037:
038: private JoiningNullTransactionImpl newTransaction(
039: Transaction transaction) {
040: return new JoiningNullTransactionImpl(transaction);
041: }
042:
043: /**
044: * Does nothing
045: */
046: public void commit(Transaction transaction) throws SQLException {
047: }
048:
049: /**
050: * Does nothing
051: */
052: public void rollback(Transaction transaction) throws SQLException {
053: }
054:
055: /**
056: * Does nothing
057: */
058: public void rollbackSilently(Transaction transaction) {
059: }
060:
061: /**
062: * Does nothing
063: */
064: public void release(Transaction transaction) throws SQLException {
065: // Do nothing
066: }
067:
068: public Transaction beginUnchecked() {
069: try {
070: return begin();
071: } catch (SQLException e) {
072: throw new OdalRuntimeException(e.getClass().getName()
073: + ": " + e.getMessage());
074: }
075: }
076:
077: public void commitUnchecked(Transaction transaction) {
078: try {
079: commit(transaction);
080: } catch (SQLException e) {
081: throw new OdalRuntimeException(e.getClass().getName()
082: + ": " + e.getMessage());
083: }
084: }
085:
086: public void rollbackUnchecked(Transaction transaction) {
087: try {
088: rollback(transaction);
089: } catch (SQLException e) {
090: throw new OdalRuntimeException(e.getClass().getName()
091: + ": " + e.getMessage());
092: }
093: }
094:
095: public void releaseUnchecked(Transaction transaction) {
096: try {
097: release(transaction);
098: } catch (SQLException e) {
099: throw new OdalRuntimeException(e.getClass().getName()
100: + ": " + e.getMessage());
101: }
102: }
103:
104: /**
105: * Does nothing
106: */
107: public void shutdown() {
108: // Do nothing
109: }
110:
111: /**
112: * No guarantee that the method returns the same instance as was receive with begin() method
113: * since Current Transaction is just a wrapper for the "real" one
114: *
115: * @see com.completex.objective.components.persistency.transact.TransactionManager#getCurrentTransaction()
116: */
117: public Transaction getCurrentTransaction() {
118: Transaction coreTransaction = coreTransactionManager
119: .getCurrentTransaction();
120: return coreTransaction == null ? null
121: : newTransaction(coreTransaction);
122: }
123:
124: public boolean hasCurrentTransaction() {
125: return coreTransactionManager.hasCurrentTransaction();
126: }
127:
128: private Transaction getCurrentCoreTransactionSafe()
129: throws OdalPersistencyException {
130: Transaction currentTransaction = coreTransactionManager
131: .getCurrentTransaction();
132: if (currentTransaction == null) {
133: throw new OdalPersistencyException(
134: "No currentTransaction found: open transaction first");
135: }
136: return currentTransaction;
137: }
138:
139: }
|