KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > resource > MySynchro


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: MySynchro.java,v 1.16 2005/04/28 08:43:25 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas.resource;
28
29 import javax.transaction.Synchronization JavaDoc;
30
31 import org.objectweb.util.monolog.api.BasicLevel;
32
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * This class is an implementation of the Synchronization interface, used by a
38  * ConnectionManagerImpl instance. The aim of this class is to close a
39  * ManagedConnection when a transaction finishes. This class is necessary when
40  * the commit or the rollback action is after a connection close.
41  *
42  *@author sebastien.chassande@inrialpes.fr
43  *@author Eric.Hardesty@bull.com
44  */

45 public class MySynchro implements Synchronization JavaDoc {
46
47     /**
48      * The ManagedConnection descriptor linked to this Synchronization
49      * implementation
50      */

51     private MCInfo mci = null;
52     /**
53      * The ConnectionManagerImpl instance which manages this synchronization
54      */

55     private ConnectionManagerImpl cm = null;
56
57
58     /**
59      * Constructor for the MySynchro object
60      *
61      *@param pCm ConnectionManager object
62      *@param pMci ManagedConnection information object
63      *
64      */

65     public MySynchro(ConnectionManagerImpl pCm, MCInfo pMci) {
66         mci = pMci;
67         cm = pCm;
68         mci.synchro = this;
69         cm.synchros.add(mci);
70     }
71
72
73     /**
74      * This method is called by the transaction manager after the transaction is
75      * committed or rolled back. Release the mci from the pool
76      *
77      *@param status The transaction flag
78      */

79     public void afterCompletion(int status) {
80         if (mci == null) {
81             return;
82         }
83         synchronized (cm) {
84             mci.synchro = null;
85             cm.synchros.remove(mci);
86             if (!mci.usedCs.isEmpty()) {
87                 // The transaction is finished but the connection isn't closed
88
// then just to dissociate the tx to the mci
89
cm.usedMCs.remove(mci.ctx);
90                 mci.ctx = null;
91                 if (cm.poolTrace.isLoggable(BasicLevel.DEBUG)) {
92                     cm.poolTrace.log(BasicLevel.DEBUG, "state:\n" + cm.getState("\t"));
93                 }
94             } else {
95                 // The transaction is finished and the logical connections have
96
// previously closed. Then release the mci
97
try {
98                     cm.mcs.remove(mci);
99                     if (mci.ctx != null) {
100                         cm.usedMCs.remove(mci.ctx);
101                     } else {
102                         Iterator JavaDoc it = cm.usedMCs.entrySet().iterator();
103                         while (it.hasNext()) {
104                             Map.Entry JavaDoc me = (Map.Entry JavaDoc) it.next();
105                             if (mci.equals(me.getValue())) {
106                                 it.remove();
107                             }
108                         }
109                     }
110
111                     mci.ctx = null;
112                     mci.mc.cleanup();
113
114                     cm.poolMCs.releaseResource(mci.mc, false, false);
115                     if (ConnectionManagerImpl.trace.isLoggable(BasicLevel.DEBUG)) {
116                         ConnectionManagerImpl.trace.log(BasicLevel.DEBUG, "Later releasing of MC=" + mci.mc);
117                     }
118                     mci = null;
119                     if (ConnectionManagerImpl.poolTrace.isLoggable(BasicLevel.DEBUG)) {
120                         ConnectionManagerImpl.poolTrace.log(BasicLevel.DEBUG, "state\n" + cm.getState("\t"));
121                     }
122                 } catch (Exception JavaDoc e) {
123                     ConnectionManagerImpl.trace.log(BasicLevel.ERROR,
124                             "an error related during releasing of ManagedConection",
125                             e, "MySynchro", "afterCompletion");
126                 }
127             }
128             mci = null;
129             cm = null;
130         }
131     }
132
133
134     /**
135      * This method is called by the transaction manager prior to the start of the
136      * transaction completion process.
137      */

138     public void beforeCompletion() {
139     }
140 }
141
142
Popular Tags