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