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.TransactionListener;
008:
009: import java.sql.Connection;
010: import java.sql.SQLException;
011: import java.util.Collections;
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.List;
015: import java.util.Map;
016:
017: /**
018: * @author Gennady Krizhevsky
019: */
020: public abstract class AbstractTransactionImpl implements Transaction {
021: private Log logger = Log.NULL_LOGGER;
022: private final Connection connection;
023: private String connectionToString;
024: private HashMap listeners;
025:
026: public AbstractTransactionImpl(Connection connection, Log logger) {
027: this .connection = connection;
028: this .connectionToString = connectionToString(connection);
029: setLogger(logger);
030: }
031:
032: public AbstractTransactionImpl(Connection connection) {
033: this (connection, null);
034: }
035:
036: private String connectionToString(Connection connection) {
037: String connectionString = null;
038: if (connection != null) {
039: connectionString = connection.getClass().getName() + "@"
040: + Integer.toHexString(connection.hashCode());
041: }
042: return connectionString;
043: }
044:
045: public Log getLogger() {
046: return logger;
047: }
048:
049: public void setLogger(Log logger) {
050: if (logger != null) {
051: this .logger = logger;
052: }
053: }
054:
055: public String connectionToString() {
056: return connectionToString;
057: }
058:
059: public Connection getConnection() {
060: return connection;
061: }
062:
063: public void commit() throws OdalPersistencyException {
064: try {
065: try {
066: connection.commit();
067: } catch (SQLException e) {
068: throw new OdalPersistencyException(e);
069: }
070: if (logger.isDebugEnabled()) {
071: logger
072: .debug("COMMIT TRANSACTION "
073: + connectionToString);
074: }
075: } finally {
076: try {
077: if (listeners != null) {
078: for (Iterator it = listeners.entrySet().iterator(); it
079: .hasNext();) {
080: Map.Entry entry = (Map.Entry) it.next();
081: TransactionListener listener = (TransactionListener) entry
082: .getValue();
083: listener.afterCommit();
084: }
085: }
086: } finally {
087: clearListerners();
088: }
089: }
090: }
091:
092: public List flush() throws OdalPersistencyException {
093: return Collections.EMPTY_LIST;
094: }
095:
096: public void rollback() throws OdalPersistencyException {
097: try {
098: try {
099: connection.rollback();
100: } catch (SQLException e) {
101: throw new OdalPersistencyException(e);
102: }
103: if (logger.isDebugEnabled()) {
104: logger.debug("ROLLBACK TRANSACTION "
105: + connectionToString);
106: }
107: } finally {
108: try {
109: if (listeners != null) {
110: for (Iterator it = listeners.entrySet().iterator(); it
111: .hasNext();) {
112: Map.Entry entry = (Map.Entry) it.next();
113: TransactionListener listener = (TransactionListener) entry
114: .getValue();
115: listener.afterRollback();
116: }
117: }
118: } finally {
119: clearListerners();
120: }
121: }
122: }
123:
124: public void commitUnchecked() {
125: try {
126: commit();
127: } catch (SQLException e) {
128: throw new OdalRuntimeException(e.getClass().getName()
129: + ": " + e.getMessage());
130: }
131: }
132:
133: public void rollbackUnchecked() {
134: try {
135: rollback();
136: } catch (SQLException e) {
137: throw new OdalRuntimeException(e.getClass().getName()
138: + ": " + e.getMessage());
139: }
140: }
141:
142: public void rollbackSilently() {
143: try {
144: rollback();
145: } catch (Exception e) {
146: // Do nothing
147: }
148: }
149:
150: public void addListerner(Object key, TransactionListener listener) {
151: if (key == null || listener == null) {
152: return;
153: }
154: HashMap listeners = lazyListeners();
155: if (listeners.containsKey(key)) {
156: throw new IllegalArgumentException("Listener with key "
157: + key + " already registerd with this transaction");
158: } else {
159: listeners.put(key, listener);
160: }
161: }
162:
163: public TransactionListener getListener(Object key) {
164: if (listeners != null) {
165: return (TransactionListener) listeners.get(key);
166: }
167: return null;
168: }
169:
170: public boolean containsListener(Object key) {
171: return getListener(key) != null;
172: }
173:
174: public void removeListerner(Object key) {
175: if (listeners != null) {
176: listeners.remove(key);
177: }
178: }
179:
180: public void clearListerners() {
181: if (listeners != null) {
182: listeners.clear();
183: listeners = null;
184: }
185: }
186:
187: public int listenersSize() {
188: return listeners == null ? 0 : listeners.size();
189: }
190:
191: private HashMap lazyListeners() {
192: if (listeners == null) {
193: listeners = new HashMap();
194: }
195: return listeners;
196: }
197: }
|