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 java.util.HashMap;
020: import java.util.Map;
021:
022: import org.compass.core.CompassException;
023: import org.compass.core.CompassSession;
024: import org.compass.core.CompassTransaction;
025: import org.compass.core.CompassTransaction.TransactionIsolation;
026: import org.compass.core.config.CompassEnvironment;
027: import org.compass.core.config.CompassSettings;
028: import org.compass.core.spi.InternalCompassSession;
029:
030: /**
031: * @author kimchy
032: */
033: public class LocalTransactionFactory extends AbstractTransactionFactory {
034:
035: /**
036: * A ThreadLocal maintaining current sessions for the given execution thread.
037: * The actual ThreadLocal variable is a java.util.Map to account for
038: * the possibility for multiple Compass instances being used during execution
039: * of the given thread.
040: */
041: private static final ThreadLocal context = new ThreadLocal();
042:
043: private boolean disableThreadBoundTx = false;
044:
045: protected void doConfigure(CompassSettings settings) {
046: disableThreadBoundTx = settings
047: .getSettingAsBoolean(
048: CompassEnvironment.Transaction.DISABLE_THREAD_BOUND_LOCAL_TRANSATION,
049: false);
050: }
051:
052: protected boolean isWithinExistingTransaction(
053: InternalCompassSession session) throws CompassException {
054: return getTransactionBoundSession() == session;
055: }
056:
057: protected InternalCompassTransaction doBeginTransaction(
058: InternalCompassSession session,
059: TransactionIsolation transactionIsolation)
060: throws CompassException {
061: LocalTransaction tx = new LocalTransaction(session, this ,
062: transactionIsolation);
063: tx.begin();
064: return tx;
065: }
066:
067: protected InternalCompassTransaction doContinueTransaction(
068: InternalCompassSession session) throws CompassException {
069: LocalTransaction tx = new LocalTransaction(session, this , null);
070: tx.join(session);
071: return tx;
072: }
073:
074: public CompassSession getTransactionBoundSession()
075: throws CompassException {
076: if (disableThreadBoundTx) {
077: return null;
078: }
079: Map sessionMap = sessionMap();
080: if (sessionMap == null) {
081: return null;
082: } else {
083: return (CompassSession) sessionMap.get(compass);
084: }
085: }
086:
087: public void unbindSessionFromTransaction(LocalTransaction tr,
088: CompassSession session) {
089: if (disableThreadBoundTx) {
090: return;
091: }
092: Map sessionMap = sessionMap();
093: if (sessionMap != null) {
094: sessionMap.remove(compass);
095: if (sessionMap.isEmpty()) {
096: context.set(null);
097: }
098: }
099: }
100:
101: protected void doBindSessionToTransaction(CompassTransaction tr,
102: CompassSession session) throws CompassException {
103: if (disableThreadBoundTx) {
104: return;
105: }
106: Map sessionMap = sessionMap();
107: if (sessionMap == null) {
108: sessionMap = new HashMap();
109: context.set(sessionMap);
110: }
111: sessionMap.put(compass, session);
112: }
113:
114: private static Map sessionMap() {
115: return (Map) context.get();
116: }
117:
118: }
|