001: package org.apache.ojb.jdori.sql;
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 javax.jdo.JDODataStoreException;
019: import javax.jdo.JDOUserException;
020:
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:
026: import com.sun.jdori.Connector;
027:
028: /**
029: * OjbStoreConnector represents a OJB PB connection
030: *
031: * @author Thomas Mahler
032: */
033: class OjbStoreConnector implements Connector {
034: /**
035: * rollback only flag
036: */
037: private boolean rollbackOnlyFlag = false;
038:
039: /**
040: * Datasource to which this Connector writes its Message.
041: */
042: private final OjbStorePMF pmf;
043:
044: /**
045: * broker represents the backend store.
046: */
047: PersistenceBroker broker = null;
048:
049: /**
050: * if true underlying connection can be released
051: */
052: private boolean connectionReadyForRelease = true;
053:
054: /**
055: * the logger used for debugging
056: */
057: private Logger logger = LoggerFactory.getLogger("JDO");
058:
059: OjbStoreConnector(OjbStorePMF pmf) {
060: this .pmf = pmf;
061: }
062:
063: /**
064: * @see com.sun.jdori.Connector#begin
065: */
066: public void begin(boolean optimistic) {
067: assertNotRollbackOnly();
068:
069: connectionReadyForRelease = false;
070: logger
071: .debug("OjbStoreConnector.begin: connectionReadyForRelease="
072: + connectionReadyForRelease);
073:
074: // obtain a fresh broker and open a tx on it
075: broker = PersistenceBrokerFactory.defaultPersistenceBroker();
076: broker.beginTransaction();
077: }
078:
079: /**
080: * @see com.sun.jdori.Connector#beforeCompletion
081: */
082: public void beforeCompletion() {
083: assertNotRollbackOnly();
084: // Nothing to do.
085: }
086:
087: /**
088: * @see com.sun.jdori.Connector#flush
089: */
090: public void flush() {
091: assertNotRollbackOnly();
092: logger.debug("OjbStoreConnector.flush: "
093: + "connectionReadyForRelease="
094: + connectionReadyForRelease);
095: // thma: noop?
096: }
097:
098: /**
099: * @see com.sun.jdori.Connector#commit
100: */
101: public synchronized void commit() {
102: assertNotRollbackOnly();
103:
104: try {
105: logger.debug("OjbStoreConnector.commit");
106: broker.commitTransaction();
107: broker.close();
108: broker = null;
109: } catch (Exception ex) {
110: throw new OjbStoreFatalInternalException(getClass(),
111: "commit", ex);
112: } finally {
113: connectionReadyForRelease = true;
114: }
115: }
116:
117: /**
118: * @see com.sun.jdori.Connector#rollback
119: */
120: public synchronized void rollback() {
121: logger.debug("OjbStoreConnector.rollback");
122:
123: if (!rollbackOnlyFlag) {
124: try {
125: broker.abortTransaction();
126: broker.close();
127: broker = null;
128: } catch (Exception ex) {
129: throw new OjbStoreFatalInternalException(getClass(),
130: "rollback", ex);
131: } finally {
132: connectionReadyForRelease = true;
133: }
134: }
135: }
136:
137: /**
138: * @see com.sun.jdori.Connector#setRollbackOnly
139: */
140: public void setRollbackOnly() {
141: rollbackOnlyFlag = true;
142: }
143:
144: /**
145: * @see com.sun.jdori.Connector#getRollbackOnly
146: */
147: public boolean getRollbackOnly() {
148: return rollbackOnlyFlag;
149: }
150:
151: private void assertNotRollbackOnly() {
152: if (rollbackOnlyFlag) {
153: throw new JDODataStoreException("Rollback Only !");
154: }
155: }
156:
157: /**
158: * Returns the broker.
159: * @return PersistenceBroker
160: */
161: public PersistenceBroker getBroker() {
162: if (broker == null) {
163: throw new JDOUserException("No transaction in progress.");
164: }
165: return broker;
166: }
167:
168: /**
169: * Sets the broker.
170: * @param broker The broker to set
171: */
172: public void setBroker(PersistenceBroker broker) {
173: this.broker = broker;
174: }
175:
176: }
|