KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > transaction > ManagedTransactionFactory


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 java.util.HashMap JavaDoc;
19
20 import javax.transaction.SystemException JavaDoc;
21 import javax.transaction.TransactionManager JavaDoc;
22
23 import org.apache.ojb.broker.PBKey;
24 import org.apache.ojb.broker.transaction.tm.TransactionManagerFactoryFactory;
25 import org.apache.ojb.broker.transaction.tm.TransactionManagerFactoryException;
26 import org.apache.ojb.otm.OTMConnection;
27 import org.apache.ojb.otm.core.BaseConnection;
28 import org.apache.ojb.otm.core.Transaction;
29 import org.apache.ojb.otm.core.TransactionException;
30
31 /**
32  * Factory for OTM Transactions within a managed environment (JTA).
33  *
34  * @author <a HREF="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
35  *
36  */

37 public abstract class ManagedTransactionFactory implements TransactionFactory
38 {
39
40     private HashMap JavaDoc _transactionMap;
41     private TransactionManager JavaDoc tm;
42
43     /**
44      * Constructor for ManagedTransactionFactory.
45      */

46     public ManagedTransactionFactory()
47     {
48         _transactionMap = new HashMap JavaDoc();
49     }
50
51
52     //////////////////////////////////////////////////////
53
// TransactionFactory protocol
54
//////////////////////////////////////////////////////
55

56     /**
57      * @see org.apache.ojb.otm.transaction.TransactionFactory#getTransactionForConnection(OTMConnection)
58      */

59     public Transaction getTransactionForConnection(OTMConnection connection)
60     {
61         if (!(connection instanceof BaseConnection))
62         {
63             throw new TransactionFactoryException("Unknown connection type");
64         }
65         BaseConnection baseConnection = (BaseConnection) connection;
66
67         javax.transaction.Transaction JavaDoc jtaTx = getJTATransaction();
68         if (jtaTx == null)
69         {
70             throw new TransactionFactoryException("Unable to get the JTA Transaction");
71         }
72
73         Transaction tx = (Transaction) _transactionMap.get(jtaTx);
74         if (tx == null)
75         {
76             tx = new Transaction();
77             _transactionMap.put(jtaTx, tx);
78         }
79
80         // ensure that this connection is registered into this transaction
81
tx.registerConnection(baseConnection);
82         return tx;
83     }
84
85     /**
86      * @see org.apache.ojb.otm.transaction.TransactionFactory#acquireConnection
87      */

88     public OTMConnection acquireConnection(PBKey pbKey)
89     {
90         return new ManagedConnection(pbKey);
91     }
92
93
94     //////////////////////////////////////////
95
// Other operations
96
//////////////////////////////////////////
97

98     public javax.transaction.Transaction JavaDoc getJTATransaction()
99     {
100         if(tm == null)
101         {
102             try
103             {
104                 tm = TransactionManagerFactoryFactory.instance().getTransactionManager();
105             }
106             catch (TransactionManagerFactoryException e)
107             {
108                 throw new TransactionFactoryException("Can't instantiate TransactionManagerFactory", e);
109             }
110         }
111         try
112         {
113             return tm.getTransaction();
114         }
115         catch(SystemException JavaDoc e)
116         {
117             throw new TransactionFactoryException("Error acquiring JTA Transaction", e);
118         }
119     }
120
121     private static class ManagedConnection extends BaseConnection
122     {
123
124         public ManagedConnection (PBKey pbKey)
125         {
126             super(pbKey);
127         }
128
129         /**
130          * @see org.apache.ojb.otm.core.BaseConnection#transactionBegin()
131          */

132         public void transactionBegin() throws TransactionException
133         {
134             // Nothing to do!
135
}
136
137         /**
138          * @see org.apache.ojb.otm.core.BaseConnection#transactionPrepare()
139          */

140         public void transactionPrepare() throws TransactionException
141         {
142             // Nothing to do, since all resources are managed by JTS and will be committed
143
// directly.
144
}
145
146         /**
147          * @see org.apache.ojb.otm.core.BaseConnection#transactionCommit()
148          */

149         public void transactionCommit() throws TransactionException
150         {
151             // Nothing to do, since all resources are managed by JTS and will be committed
152
// directly.
153
}
154
155         /**
156          * @see org.apache.ojb.otm.core.BaseConnection#transactionRollback()
157          */

158         public void transactionRollback() throws TransactionException
159         {
160             // Nothing to do, since all resources are managed by JTS and will be rolled back
161
// directly.
162
}
163
164     }
165 }
166
Popular Tags