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 java.util.HashMap;
019:
020: import javax.transaction.SystemException;
021: import javax.transaction.TransactionManager;
022:
023: import org.apache.ojb.broker.PBKey;
024: import org.apache.ojb.broker.transaction.tm.TransactionManagerFactoryFactory;
025: import org.apache.ojb.broker.transaction.tm.TransactionManagerFactoryException;
026: import org.apache.ojb.otm.OTMConnection;
027: import org.apache.ojb.otm.core.BaseConnection;
028: import org.apache.ojb.otm.core.Transaction;
029: import org.apache.ojb.otm.core.TransactionException;
030:
031: /**
032: * Factory for OTM Transactions within a managed environment (JTA).
033: *
034: * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
035: *
036: */
037: public abstract class ManagedTransactionFactory implements
038: TransactionFactory {
039:
040: private HashMap _transactionMap;
041: private TransactionManager tm;
042:
043: /**
044: * Constructor for ManagedTransactionFactory.
045: */
046: public ManagedTransactionFactory() {
047: _transactionMap = new HashMap();
048: }
049:
050: //////////////////////////////////////////////////////
051: // TransactionFactory protocol
052: //////////////////////////////////////////////////////
053:
054: /**
055: * @see org.apache.ojb.otm.transaction.TransactionFactory#getTransactionForConnection(OTMConnection)
056: */
057: public Transaction getTransactionForConnection(
058: OTMConnection connection) {
059: if (!(connection instanceof BaseConnection)) {
060: throw new TransactionFactoryException(
061: "Unknown connection type");
062: }
063: BaseConnection baseConnection = (BaseConnection) connection;
064:
065: javax.transaction.Transaction jtaTx = getJTATransaction();
066: if (jtaTx == null) {
067: throw new TransactionFactoryException(
068: "Unable to get the JTA Transaction");
069: }
070:
071: Transaction tx = (Transaction) _transactionMap.get(jtaTx);
072: if (tx == null) {
073: tx = new Transaction();
074: _transactionMap.put(jtaTx, tx);
075: }
076:
077: // ensure that this connection is registered into this transaction
078: tx.registerConnection(baseConnection);
079: return tx;
080: }
081:
082: /**
083: * @see org.apache.ojb.otm.transaction.TransactionFactory#acquireConnection
084: */
085: public OTMConnection acquireConnection(PBKey pbKey) {
086: return new ManagedConnection(pbKey);
087: }
088:
089: //////////////////////////////////////////
090: // Other operations
091: //////////////////////////////////////////
092:
093: public javax.transaction.Transaction getJTATransaction() {
094: if (tm == null) {
095: try {
096: tm = TransactionManagerFactoryFactory.instance()
097: .getTransactionManager();
098: } catch (TransactionManagerFactoryException e) {
099: throw new TransactionFactoryException(
100: "Can't instantiate TransactionManagerFactory",
101: e);
102: }
103: }
104: try {
105: return tm.getTransaction();
106: } catch (SystemException e) {
107: throw new TransactionFactoryException(
108: "Error acquiring JTA Transaction", e);
109: }
110: }
111:
112: private static class ManagedConnection extends BaseConnection {
113:
114: public ManagedConnection(PBKey pbKey) {
115: super (pbKey);
116: }
117:
118: /**
119: * @see org.apache.ojb.otm.core.BaseConnection#transactionBegin()
120: */
121: public void transactionBegin() throws TransactionException {
122: // Nothing to do!
123: }
124:
125: /**
126: * @see org.apache.ojb.otm.core.BaseConnection#transactionPrepare()
127: */
128: public void transactionPrepare() throws TransactionException {
129: // Nothing to do, since all resources are managed by JTS and will be committed
130: // directly.
131: }
132:
133: /**
134: * @see org.apache.ojb.otm.core.BaseConnection#transactionCommit()
135: */
136: public void transactionCommit() throws TransactionException {
137: // Nothing to do, since all resources are managed by JTS and will be committed
138: // directly.
139: }
140:
141: /**
142: * @see org.apache.ojb.otm.core.BaseConnection#transactionRollback()
143: */
144: public void transactionRollback() throws TransactionException {
145: // Nothing to do, since all resources are managed by JTS and will be rolled back
146: // directly.
147: }
148:
149: }
150: }
|