001: package org.apache.ojb.odmg;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.OJBRuntimeException;
019: import org.apache.ojb.broker.PBFactoryException;
020: import org.apache.ojb.broker.PBKey;
021: import org.apache.ojb.broker.PersistenceBroker;
022: import org.apache.ojb.broker.PersistenceBrokerFactory;
023: import org.apache.ojb.broker.util.logging.Logger;
024: import org.apache.ojb.broker.util.logging.LoggerFactory;
025: import org.odmg.Transaction;
026:
027: public final class PBCapsule {
028: private static Logger log = LoggerFactory
029: .getLogger(PBCapsule.class);
030:
031: PersistenceBroker broker;
032: PBKey pbKey;
033: Transaction tx;
034: boolean needsTxCommit = false;
035: boolean needsPBCommit = false;
036: boolean isIlleagal = false;
037:
038: public PBCapsule(final PBKey pbKey, final Transaction tx) {
039: this .tx = tx;
040: this .pbKey = pbKey;
041: prepare();
042: }
043:
044: public PersistenceBroker getBroker() {
045: if (isIlleagal)
046: throw new OJBRuntimeException(
047: "You could not reuse PBCapsule after destroy");
048: return broker;
049: }
050:
051: private void prepare() {
052: if (isIlleagal)
053: throw new OJBRuntimeException(
054: "You could not reuse PBCapsule after destroy");
055: // we allow queries even if no ODMG transaction is running.
056: // thus we use direct access to PBF via the given PBKey to
057: // get a PB instance
058: if (tx == null) {
059: if (log.isDebugEnabled())
060: log.debug("No running transaction found, try to get "
061: + "PersistenceBroker instance via PBKey "
062: + pbKey);
063: broker = obtainBroker();
064: // begin tx on the PB instance
065: if (!broker.isInTransaction()) {
066: broker.beginTransaction();
067: needsPBCommit = true;
068: }
069: } else {
070: // we allow to work with unopened transactions.
071: // we assume that such a tx is to be closed after performing the query
072: if (!tx.isOpen()) {
073: tx.begin();
074: needsTxCommit = true;
075: }
076: // obtain a broker instance from the current transaction
077: broker = ((HasBroker) tx).getBroker();
078: }
079: }
080:
081: public void destroy() {
082: if (needsTxCommit) {
083: if (log.isDebugEnabled())
084: log.debug("Indicated to commit tx");
085: tx.commit();
086: } else if (needsPBCommit) {
087: if (log.isDebugEnabled())
088: log.debug("Indicated to commit PersistenceBroker");
089: try {
090: broker.commitTransaction();
091: } finally {
092: if (broker != null)
093: broker.close();
094: }
095: }
096: isIlleagal = true;
097: needsTxCommit = false;
098: needsPBCommit = false;
099: }
100:
101: /**
102: * Used to get PB, when no tx is running.
103: */
104: private PersistenceBroker obtainBroker() {
105: PersistenceBroker _broker;
106: try {
107: if (pbKey == null) {
108: //throw new OJBRuntimeException("Not possible to do action, cause no tx runnning and no PBKey is set");
109: log
110: .warn("No tx runnning and PBKey is null, try to use the default PB");
111: _broker = PersistenceBrokerFactory
112: .defaultPersistenceBroker();
113: } else {
114: _broker = PersistenceBrokerFactory
115: .createPersistenceBroker(pbKey);
116: }
117: } catch (PBFactoryException e) {
118: log.error("Could not obtain PB for PBKey " + pbKey, e);
119: throw new OJBRuntimeException(
120: "Unexpected micro-kernel exception", e);
121: }
122: return _broker;
123: }
124: }
|