001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: MySynchro.java 7502 2005-10-13 14:01:44Z pelletib $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.resource;
027:
028: import javax.transaction.Synchronization;
029:
030: import org.objectweb.util.monolog.api.BasicLevel;
031:
032: import java.util.Iterator;
033: import java.util.Map;
034:
035: /**
036: * This class is an implementation of the Synchronization interface, used by a
037: * ConnectionManagerImpl instance. The aim of this class is to close a
038: * ManagedConnection when a transaction finishes. This class is necessary when
039: * the commit or the rollback action is after a connection close.
040: *
041: *@author sebastien.chassande@inrialpes.fr
042: *@author Eric.Hardesty@bull.com
043: */
044: public class MySynchro implements Synchronization {
045:
046: /**
047: * The ManagedConnection descriptor linked to this Synchronization
048: * implementation
049: */
050: private MCInfo mci = null;
051: /**
052: * The ConnectionManagerImpl instance which manages this synchronization
053: */
054: private ConnectionManagerImpl cm = null;
055:
056: /**
057: * Constructor for the MySynchro object
058: *
059: *@param pCm ConnectionManager object
060: *@param pMci ManagedConnection information object
061: *
062: */
063: public MySynchro(ConnectionManagerImpl pCm, MCInfo pMci) {
064: mci = pMci;
065: cm = pCm;
066: mci.synchro = this ;
067: cm.synchros.add(mci);
068: }
069:
070: /**
071: * This method is called by the transaction manager after the transaction is
072: * committed or rolled back. Release the mci from the pool
073: *
074: *@param status The transaction flag
075: */
076: public void afterCompletion(int status) {
077: if (mci == null) {
078: return;
079: }
080: synchronized (cm.poolMCs) {
081: mci.synchro = null;
082: cm.synchros.remove(mci);
083: if (!mci.usedCs.isEmpty()) {
084: // The transaction is finished but the connection isn't closed
085: // then just to dissociate the tx to the mci
086: cm.usedMCs.remove(mci.ctx);
087: mci.ctx = null;
088: if (cm.poolTrace.isLoggable(BasicLevel.DEBUG)) {
089: cm.poolTrace.log(BasicLevel.DEBUG, "state:\n"
090: + cm.getState("\t"));
091: }
092: } else {
093: // The transaction is finished and the logical connections have
094: // previously closed. Then release the mci
095: try {
096: cm.mcs.remove(mci);
097: if (mci.ctx != null) {
098: cm.usedMCs.remove(mci.ctx);
099: } else {
100: Iterator it = cm.usedMCs.entrySet().iterator();
101: while (it.hasNext()) {
102: Map.Entry me = (Map.Entry) it.next();
103: if (mci.equals(me.getValue())) {
104: it.remove();
105: }
106: }
107: }
108:
109: mci.ctx = null;
110: mci.mc.cleanup();
111:
112: cm.poolMCs.releaseResource(mci.mc, false, false);
113: if (ConnectionManagerImpl.trace
114: .isLoggable(BasicLevel.DEBUG)) {
115: ConnectionManagerImpl.trace.log(
116: BasicLevel.DEBUG,
117: "Later releasing of MC=" + mci.mc);
118: }
119: mci = null;
120: if (ConnectionManagerImpl.poolTrace
121: .isLoggable(BasicLevel.DEBUG)) {
122: ConnectionManagerImpl.poolTrace.log(
123: BasicLevel.DEBUG, "state\n"
124: + cm.getState("\t"));
125: }
126: } catch (Exception e) {
127: ConnectionManagerImpl.trace
128: .log(
129: BasicLevel.ERROR,
130: "an error related during releasing of ManagedConection",
131: e, "MySynchro", "afterCompletion");
132: }
133: }
134: mci = null;
135: cm = null;
136: }
137: }
138:
139: /**
140: * This method is called by the transaction manager prior to the start of the
141: * transaction completion process.
142: */
143: public void beforeCompletion() {
144: }
145: }
|