001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.transaction;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.compass.core.CompassException;
022: import org.compass.core.CompassSession;
023: import org.compass.core.spi.InternalCompass;
024: import org.compass.core.spi.InternalCompassSession;
025:
026: /**
027: * @author kimchy
028: */
029:
030: public class LocalTransaction extends AbstractTransaction {
031:
032: private static final Log log = LogFactory
033: .getLog(LocalTransaction.class);
034:
035: private static final int UNKNOWN = -1;
036:
037: private static final int STARTED = 0;
038:
039: private static final int COMMIT = 1;
040:
041: private static final int ROLLBACK = 2;
042:
043: private int state;
044:
045: private InternalCompassSession session;
046:
047: private InternalCompass compass;
048:
049: private TransactionIsolation transactionIsolation;
050:
051: public LocalTransaction(InternalCompassSession session,
052: TransactionFactory transactionFactory,
053: TransactionIsolation transactionIsolation) {
054: super (transactionFactory);
055: state = UNKNOWN;
056: this .session = session;
057: this .compass = (InternalCompass) session.getCompass();
058: this .transactionIsolation = transactionIsolation;
059: }
060:
061: public void join(InternalCompassSession session)
062: throws CompassException {
063: this .session = session;
064: if (log.isDebugEnabled()) {
065: log
066: .debug("Joining an existing local transcation on thread ["
067: + Thread.currentThread().getName()
068: + "] Compass ["
069: + System.identityHashCode(compass)
070: + "] Session ["
071: + System.identityHashCode(session) + "]");
072: }
073: }
074:
075: public void begin() throws CompassException {
076: if (log.isDebugEnabled()) {
077: log.debug("Starting a new local transcation on thread ["
078: + Thread.currentThread().getName() + "] Compass ["
079: + System.identityHashCode(compass) + "] Session ["
080: + System.identityHashCode(session)
081: + "] with isolation [" + transactionIsolation
082: + "]");
083: }
084: session.getSearchEngine().begin(transactionIsolation);
085: state = STARTED;
086: }
087:
088: protected void doCommit() throws CompassException {
089: if (session.getSearchEngine().wasRolledBack()) {
090: // don't do anything, since it was rolled back already
091: }
092:
093: if (state == UNKNOWN) {
094: log
095: .debug("Not committing the transaction since within a local transaction on thread ["
096: + Thread.currentThread().getName()
097: + "] Compass ["
098: + System.identityHashCode(compass)
099: + "] Session ["
100: + System.identityHashCode(session) + "]");
101: return;
102: }
103:
104: // commit called by the high level local transaction
105: if (log.isDebugEnabled()) {
106: log.debug("Committing local transaction on thread ["
107: + Thread.currentThread().getName() + "] Compass ["
108: + System.identityHashCode(compass) + "] Session ["
109: + System.identityHashCode(session) + "]");
110: }
111:
112: session.evictAll();
113: ((LocalTransactionFactory) transactionFactory)
114: .unbindSessionFromTransaction(this , session);
115: session.getSearchEngine().commit(true);
116: state = COMMIT;
117: }
118:
119: protected void doRollback() throws CompassException {
120: if (session.getSearchEngine().wasRolledBack()) {
121: // don't do anything, since it was rolled back already
122: }
123:
124: if (state == UNKNOWN) {
125: if (log.isDebugEnabled()) {
126: log
127: .debug("Rolling back local transaction, which exists within another local transaction "
128: + " on thread ["
129: + Thread.currentThread().getName()
130: + "] Compass ["
131: + System.identityHashCode(compass)
132: + "] Session ["
133: + System.identityHashCode(session)
134: + "]");
135: }
136: } else {
137: if (log.isDebugEnabled()) {
138: log.debug("Rolling back local transaction on thread ["
139: + Thread.currentThread().getName()
140: + "] Compass ["
141: + System.identityHashCode(compass)
142: + "] Session ["
143: + System.identityHashCode(session) + "]");
144: }
145:
146: ((LocalTransactionFactory) transactionFactory)
147: .unbindSessionFromTransaction(this , session);
148: }
149:
150: state = ROLLBACK;
151: session.evictAll();
152: session.getSearchEngine().rollback();
153: }
154:
155: public boolean wasRolledBack() throws CompassException {
156: return session.getSearchEngine().wasRolledBack();
157: }
158:
159: public boolean wasCommitted() throws CompassException {
160: return session.getSearchEngine().wasCommitted();
161: }
162:
163: public CompassSession getSession() {
164: return this.session;
165: }
166: }
|