001: package com.completex.objective.components.persistency.transact.impl;
002:
003: import com.completex.objective.components.OdalRuntimeException;
004: import com.completex.objective.components.log.Log;
005: import com.completex.objective.components.persistency.OdalPersistencyException;
006: import com.completex.objective.components.persistency.transact.Transaction;
007: import com.completex.objective.components.persistency.transact.TransactionManager;
008:
009: import java.sql.SQLException;
010:
011: /**
012: * Transaction manager implementation that always joins current transaction if it exists but never releases
013: * the transaction to the pool
014: *
015: * @author Gennady Krizhevsky
016: */
017: public class JoiningTransactionManagerImpl implements
018: TransactionManager {
019:
020: private final TransactionManager coreTransactionManager;
021: private Log logger = Log.NULL_LOGGER;
022:
023: public JoiningTransactionManagerImpl(
024: TransactionManager coreTransactionManager) {
025: this .coreTransactionManager = coreTransactionManager;
026: }
027:
028: public void setLogger(Log logger) {
029: if (logger != null) {
030: this .logger = logger;
031: }
032: }
033:
034: public Log getLogger() {
035: return logger;
036: }
037:
038: /**
039: * Returns current transaction
040: *
041: * @return current transaction
042: * @throws com.completex.objective.components.persistency.OdalPersistencyException if no current transaction found
043: */
044: public Transaction begin() throws SQLException {
045: return getCurrentCoreTransactionSafe();
046: }
047:
048: /**
049: * Commits current transaction
050: *
051: * @param transaction ignored
052: * @throws SQLException
053: */
054: public void commit(Transaction transaction) throws SQLException {
055: getCurrentCoreTransactionSafe().commit();
056: }
057:
058: /**
059: * Rolls back current transaction
060: *
061: * @param transaction ignored
062: * @throws SQLException
063: */
064: public void rollback(Transaction transaction) throws SQLException {
065: getCurrentCoreTransactionSafe().rollback();
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: public void rollbackSilently(Transaction transaction) {
105: if (transaction != null) {
106: try {
107: rollback(transaction);
108: } catch (Exception e) {
109: // Silently!
110: getLogger().error("", e);
111: }
112: }
113: }
114:
115: /**
116: * Does nothing
117: */
118: public void release(Transaction transaction) throws SQLException {
119: // Do nothing
120: }
121:
122: /**
123: * Does nothing
124: */
125: public void shutdown() {
126: // Do nothing
127: }
128:
129: /**
130: * @see com.completex.objective.components.persistency.transact.TransactionManager#getCurrentTransaction()
131: */
132: public Transaction getCurrentTransaction() {
133: return coreTransactionManager.getCurrentTransaction();
134: }
135:
136: public boolean hasCurrentTransaction() {
137: return coreTransactionManager.hasCurrentTransaction();
138: }
139:
140: private Transaction getCurrentCoreTransactionSafe()
141: throws OdalPersistencyException {
142: Transaction currentTransaction = coreTransactionManager
143: .getCurrentTransaction();
144: if (currentTransaction == null) {
145: throw new OdalPersistencyException(
146: "No currentTransaction found: open transaction first");
147: }
148: return currentTransaction;
149: }
150:
151: }
|