001: /*
002: * $Id: TransactionManagerImpl.java,v 1.20 2005/12/20 18:32:46 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.axiondb.AxionException;
050: import org.axiondb.Database;
051: import org.axiondb.Transaction;
052: import org.axiondb.TransactionConflictException;
053: import org.axiondb.TransactionManager;
054: import org.axiondb.util.Utils;
055:
056: /**
057: * An implemenation of {@link TransactionManager}currently only supportd
058: * {@link SnapshotIsolationTransaction}s.
059: *
060: * @version $Revision: 1.20 $ $Date: 2005/12/20 18:32:46 $
061: * @author Rodney Waldhoff
062: */
063: public class TransactionManagerImpl implements TransactionManager {
064: public TransactionManagerImpl(Database db) {
065: _database = db;
066: }
067:
068: public synchronized Transaction createTransaction()
069: throws AxionException {
070: assertNotShutdown();
071: Transaction t = new SnapshotIsolationTransaction(
072: getLastCommittedTransaction());
073: _openTransactions.add(t);
074: return t;
075: }
076:
077: public synchronized void commitTransaction(Transaction t)
078: throws AxionException {
079: assertNotShutdown();
080: if (t.getModifiedTables().isEmpty()) {
081: t.commit();
082: t.apply();
083: _openTransactions.remove(t);
084: } else {
085: // check for conflicts
086: Iterator iter = null;
087: {
088: int index = _committedTransactions.indexOf(t
089: .getOpenOnTransaction());
090: if (-1 == index) {
091: iter = _committedTransactions.iterator();
092: } else {
093: iter = _committedTransactions.subList(index + 1,
094: _committedTransactions.size()).iterator();
095: }
096: }
097:
098: while (iter.hasNext()) {
099: Transaction c = (Transaction) (iter.next());
100: if (inConflict(t, c)) {
101: _log.debug("commitTransaction(): conflict found.");
102: throw new TransactionConflictException(
103: "Transaction conflict.");
104: }
105: }
106:
107: t.commit();
108: _committedTransactions.add(t);
109: _openTransactions.remove(t);
110: }
111: tryToApply();
112: }
113:
114: public synchronized void abortTransaction(Transaction t)
115: throws AxionException {
116: assertNotShutdown();
117: t.rollback();
118: _openTransactions.remove(t);
119:
120: try {
121: tryToApply();
122: } catch (AxionException e) {
123: if (_log.isDebugEnabled()) {
124: _log.debug(e);
125: }
126: }
127: }
128:
129: public synchronized void shutdown() throws AxionException {
130: for (int i = _openTransactions.size() - 1; i >= 0; i--) {
131: Transaction t = (Transaction) (_openTransactions.get(i));
132: t.rollback();
133: }
134: _openTransactions.clear();
135:
136: try {
137: tryToApply();
138: } catch (AxionException e) {
139: }
140: _database.shutdown();
141: _database = null;
142: }
143:
144: public synchronized boolean isShutdown() {
145: return null == _database;
146: }
147:
148: private boolean inConflict(Transaction newT, Transaction oldT) {
149: // if the new transaction changed anything
150: if (!newT.getModifiedTables().isEmpty()) {
151: // then check that none of the tables read were changed
152: // by the already committed transaction
153: // (note that this is much too strong of a conflict detection
154: // alogrithm, it'll give a lot of false conflicts).
155: if (Utils.containsAny(newT.getReadTables(), oldT
156: .getModifiedTables())) {
157: return true;
158: }
159: }
160: return false;
161: }
162:
163: private void assertNotShutdown() throws AxionException {
164: if (isShutdown()) {
165: throw new AxionException("Already shutdown");
166: }
167: }
168:
169: private void tryToApply() throws AxionException {
170: if ((!NEVER_APPLY) && _openTransactions.isEmpty()) {
171: Transaction last = null;
172: try {
173: for (Iterator iter = _committedTransactions.iterator(); iter
174: .hasNext();) {
175: last = (Transaction) (iter.next());
176: last.apply();
177: iter.remove();
178: }
179: } catch (Exception e) {
180: _committedTransactions.clear();
181: throw new AxionException("Fail to apply transction", e);
182: }
183: if (null != last) {
184: last.checkpoint();
185: }
186: }
187: }
188:
189: private Database getLastCommittedTransaction() {
190: if (_committedTransactions.isEmpty()) {
191: return _database;
192: }
193: return (Database) (_committedTransactions
194: .get(_committedTransactions.size() - 1));
195: }
196:
197: private List _committedTransactions = new ArrayList();
198: private List _openTransactions = new ArrayList();
199: private Database _database = null;
200:
201: // allow a System property to indicate that we should never apply a transaction
202: public static final boolean NEVER_APPLY;
203: static {
204: boolean neverApply = false;
205: try {
206: neverApply = Boolean
207: .getBoolean("org.axiondb.engine.TransactionManagerImpl.NEVER_APPLY");
208: } catch (Throwable t) {
209: neverApply = false;
210: }
211: NEVER_APPLY = neverApply;
212: }
213:
214: private static Log _log = LogFactory
215: .getLog(TransactionManagerImpl.class);
216:
217: }
|