001: package org.apache.ojb.otm.transaction;
002:
003: /* Copyright 2003-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.PBKey;
019: import org.apache.ojb.otm.OTMConnection;
020: import org.apache.ojb.otm.core.BaseConnection;
021: import org.apache.ojb.otm.core.Transaction;
022: import org.apache.ojb.otm.core.TransactionException;
023:
024: import java.util.HashMap;
025:
026: /**
027: *
028: * Factory for local transactions. Each OTMConnection is associated with exactly one transaction.
029: *
030: * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
031: *
032: */
033: public class LocalTransactionFactory implements TransactionFactory {
034:
035: private HashMap _transactionMap;
036:
037: public LocalTransactionFactory() {
038: _transactionMap = new HashMap();
039: }
040:
041: /**
042: * @see org.apache.ojb.otm.transaction.TransactionFactory#getTransactionForConnection(OTMConnection)
043: */
044: public Transaction getTransactionForConnection(
045: OTMConnection connection) {
046: if (!(connection instanceof BaseConnection)) {
047: StringBuffer msg = new StringBuffer();
048: msg.append("Unknown connection type: ");
049: if (connection != null)
050: msg.append(connection.getClass().getName());
051: else
052: msg
053: .append(" null. Make sure you pass a non-null OTMConnection to this method. An OTMConnection can be acquired by calling acquireConnection (PBKey pbKey)");
054: throw new TransactionFactoryException(msg.toString());
055: }
056:
057: Transaction tx = (Transaction) _transactionMap.get(connection);
058: if (tx == null) {
059: tx = new Transaction();
060: _transactionMap.put(connection, tx);
061: }
062: // ensure that this connection is registered into this transaction
063: tx.registerConnection(connection);
064: return tx;
065: }
066:
067: /**
068: * @see org.apache.ojb.otm.transaction.TransactionFactory#acquireConnection(PBKey)
069: */
070: public OTMConnection acquireConnection(PBKey pbKey) {
071: OTMConnection newConnection = new LocalConnection(pbKey);
072: // Ensure the transaction is established for this connection.
073: getTransactionForConnection(newConnection);
074: return newConnection;
075: }
076:
077: /**
078: *
079: * Represents a local connection. This is a private static inner class to restrict visibility
080: * to others.
081: *
082: * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
083: *
084: */
085: private static class LocalConnection extends BaseConnection {
086: public LocalConnection(PBKey pbKey) {
087: super (pbKey);
088: }
089:
090: /**
091: * @see org.apache.ojb.otm.core.BaseConnection#transactionBegin()
092: */
093: public void transactionBegin() throws TransactionException {
094: getKernelBroker().beginTransaction();
095: }
096:
097: /**
098: * @see org.apache.ojb.otm.core.BaseConnection#transactionPrepare()
099: */
100: public void transactionPrepare() throws TransactionException {
101: // Nothing to do!
102: }
103:
104: /**
105: * @see org.apache.ojb.otm.core.BaseConnection#transactionCommit()
106: */
107: public void transactionCommit() throws TransactionException {
108: getKernelBroker().commitTransaction();
109: }
110:
111: /**
112: * @see org.apache.ojb.otm.core.BaseConnection#transactionRollback()
113: */
114: public void transactionRollback() throws TransactionException {
115: getKernelBroker().abortTransaction();
116: }
117:
118: }
119: }
|