| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LocalTransactionFactory |
|
| 1.5;1.5 | ||||
| LocalTransactionFactory$LocalConnection |
|
| 1.5;1.5 |
| 1 | package org.apache.ojb.otm.transaction; | |
| 2 | ||
| 3 | /* Copyright 2003-2005 The Apache Software Foundation | |
| 4 | * | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 6 | * you may not use this file except in compliance with the License. | |
| 7 | * You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | ||
| 18 | import org.apache.ojb.broker.PBKey; | |
| 19 | import org.apache.ojb.otm.OTMConnection; | |
| 20 | import org.apache.ojb.otm.core.BaseConnection; | |
| 21 | import org.apache.ojb.otm.core.Transaction; | |
| 22 | import org.apache.ojb.otm.core.TransactionException; | |
| 23 | ||
| 24 | import java.util.HashMap; | |
| 25 | ||
| 26 | /** | |
| 27 | * | |
| 28 | * Factory for local transactions. Each OTMConnection is associated with exactly one transaction. | |
| 29 | * | |
| 30 | * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a> | |
| 31 | * | |
| 32 | */ | |
| 33 | public class LocalTransactionFactory implements TransactionFactory | |
| 34 | { | |
| 35 | ||
| 36 | private HashMap _transactionMap; | |
| 37 | ||
| 38 | public LocalTransactionFactory () | |
| 39 | { | |
| 40 | _transactionMap = new HashMap(); | |
| 41 | } | |
| 42 | ||
| 43 | /** | |
| 44 | * @see org.apache.ojb.otm.transaction.TransactionFactory#getTransactionForConnection(OTMConnection) | |
| 45 | */ | |
| 46 | public Transaction getTransactionForConnection (OTMConnection connection) | |
| 47 | { | |
| 48 | if (!(connection instanceof BaseConnection)) | |
| 49 | { | |
| 50 | StringBuffer msg = new StringBuffer(); | |
| 51 | msg.append("Unknown connection type: "); | |
| 52 | if (connection != null) | |
| 53 | msg.append(connection.getClass().getName()); | |
| 54 | else | |
| 55 | msg.append(" null. Make sure you pass a non-null OTMConnection to this method. An OTMConnection can be acquired by calling acquireConnection (PBKey pbKey)"); | |
| 56 | throw new TransactionFactoryException(msg.toString()); | |
| 57 | } | |
| 58 | ||
| 59 | Transaction tx = (Transaction) _transactionMap.get(connection); | |
| 60 | if (tx == null) | |
| 61 | { | |
| 62 | tx = new Transaction(); | |
| 63 | _transactionMap.put(connection, tx); | |
| 64 | } | |
| 65 | // ensure that this connection is registered into this transaction | |
| 66 | tx.registerConnection(connection); | |
| 67 | return tx; | |
| 68 | } | |
| 69 | ||
| 70 | ||
| 71 | /** | |
| 72 | * @see org.apache.ojb.otm.transaction.TransactionFactory#acquireConnection(PBKey) | |
| 73 | */ | |
| 74 | public OTMConnection acquireConnection (PBKey pbKey) | |
| 75 | { | |
| 76 | OTMConnection newConnection = new LocalConnection(pbKey); | |
| 77 | // Ensure the transaction is established for this connection. | |
| 78 | getTransactionForConnection(newConnection); | |
| 79 | return newConnection; | |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * | |
| 84 | * Represents a local connection. This is a private static inner class to restrict visibility | |
| 85 | * to others. | |
| 86 | * | |
| 87 | * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a> | |
| 88 | * | |
| 89 | */ | |
| 90 | private static class LocalConnection extends BaseConnection | |
| 91 | { | |
| 92 | public LocalConnection (PBKey pbKey) | |
| 93 | { | |
| 94 | super(pbKey); | |
| 95 | } | |
| 96 | ||
| 97 | ||
| 98 | /** | |
| 99 | * @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 | } |