KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > TransactionEnlistingInterceptor


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 package org.apache.geronimo.connector.outbound;
19
20 import javax.resource.ResourceException JavaDoc;
21 import javax.transaction.RollbackException JavaDoc;
22 import javax.transaction.SystemException JavaDoc;
23 import javax.transaction.Transaction JavaDoc;
24 import javax.transaction.TransactionManager JavaDoc;
25 import javax.transaction.xa.XAResource JavaDoc;
26
27 /**
28  * TransactionEnlistingInterceptor.java
29  * <p/>
30  * <p/>
31  * Created: Fri Sep 26 14:52:24 2003
32  *
33  * @version 1.0
34  */

35 public class TransactionEnlistingInterceptor implements ConnectionInterceptor {
36
37     private final ConnectionInterceptor next;
38     private final TransactionManager JavaDoc transactionManager;
39
40     public TransactionEnlistingInterceptor(ConnectionInterceptor next, TransactionManager JavaDoc transactionManager) {
41         this.next = next;
42         this.transactionManager = transactionManager;
43     }
44
45     public void getConnection(ConnectionInfo connectionInfo) throws ResourceException JavaDoc {
46         next.getConnection(connectionInfo);
47         try {
48             ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo();
49
50             // get the current transation and status... if there is a problem just assume there is no transaction present
51
Transaction JavaDoc transaction = TxUtil.getTransactionIfActive(transactionManager);
52             if (transaction != null) {
53                 XAResource JavaDoc xares = mci.getXAResource();
54                 transaction.enlistResource(xares);
55             }
56         } catch (SystemException JavaDoc e) {
57             returnConnection(connectionInfo, ConnectionReturnAction.DESTROY);
58             throw new ResourceException JavaDoc("Could not get transaction", e);
59         } catch (RollbackException JavaDoc e) {
60             //transaction is marked rolled back, so the xaresource could not have been enlisted
61
next.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE);
62             throw new ResourceException JavaDoc("Could not enlist resource in rolled back transaction", e);
63         } catch (Throwable JavaDoc t) {
64             returnConnection(connectionInfo, ConnectionReturnAction.DESTROY);
65             throw new ResourceException JavaDoc("Unknown throwable when trying to enlist connection in tx", t);
66         }
67     }
68
69     /**
70      * The <code>returnConnection</code> method
71      * <p/>
72      * todo Probably the logic needs improvement if a connection
73      * error occurred and we are destroying the handle.
74      *
75      * @param connectionInfo a <code>ConnectionInfo</code> value
76      * @param connectionReturnAction a <code>ConnectionReturnAction</code> value
77      */

78     public void returnConnection(ConnectionInfo connectionInfo,
79                                  ConnectionReturnAction connectionReturnAction) {
80         try {
81             ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo();
82             Transaction JavaDoc transaction = TxUtil.getTransactionIfActive(transactionManager);
83             if (transaction != null) {
84                 XAResource JavaDoc xares = mci.getXAResource();
85                 transaction.delistResource(xares, XAResource.TMSUSPEND);
86             }
87
88         } catch (SystemException JavaDoc e) {
89             //maybe we should warn???
90
connectionReturnAction = ConnectionReturnAction.DESTROY;
91         } catch (IllegalStateException JavaDoc e) {
92             connectionReturnAction = ConnectionReturnAction.DESTROY;
93         }
94
95         next.returnConnection(connectionInfo, connectionReturnAction);
96     }
97
98     public void destroy() {
99         next.destroy();
100     }
101
102 }
103
Popular Tags