XAConnectionImpl.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * --------------------------------------------------------------------------
00022  * $Id: XAConnectionImpl.java,v 1.10 2004/09/09 08:52:45 durieuxp Exp $
00023  * --------------------------------------------------------------------------
00024  */
00025 
00026 
00027 package org.objectweb.jonas.jdbc_xa;
00028 
00029 import java.sql.Connection;
00030 import java.sql.SQLException;
00031 import java.util.Vector;
00032 import javax.transaction.xa.XAResource;
00033 import javax.sql.ConnectionEvent;
00034 import javax.sql.ConnectionEventListener;
00035 import javax.sql.XAConnection;
00036 import org.objectweb.jonas.common.Log;
00037 import org.objectweb.util.monolog.api.Logger;
00038 import org.objectweb.util.monolog.api.BasicLevel;
00039 
00050 class XAConnectionImpl implements XAConnection {
00051 
00052     static private Logger logger = null;
00053 
00054     XAResource xaRes = null;
00055     Connection implConn = null;
00056     Connection actConn = null;
00057     Vector eventListeners = new Vector();
00058 
00059     // -----------------------------------------------------------------
00060     // Constructors
00061     // -----------------------------------------------------------------
00062 
00063     public XAConnectionImpl(Connection conn, XADataSourceImpl ds) {
00064 
00065         this.actConn = conn;
00066 
00067         // An XAConnection holds 2 objects: 1 Connection + 1 XAResource
00068         this.implConn = new ConnectionImpl(this, conn);
00069         this.xaRes = new XAResourceImpl(this, conn, ds);
00070         logger = Log.getLogger(Log.JONAS_JDBCXA_PREFIX);
00071         logger.log(BasicLevel.DEBUG, "constructor");
00072     }
00073 
00074     // -----------------------------------------------------------------
00075     // XAConnection implementation
00076     // -----------------------------------------------------------------
00077 
00085     public XAResource getXAResource() throws SQLException {
00086         return xaRes;
00087     }
00088 
00089     // -----------------------------------------------------------------
00090     // PooledConnection implementation
00091     // -----------------------------------------------------------------
00092 
00098     public Connection getConnection() throws SQLException {
00099 
00100         // Just return the already created object.
00101         return implConn;
00102     }
00103 
00109     public void close() throws SQLException {
00110 
00111         logger.log(BasicLevel.DEBUG, "");
00112 
00113         // Close the actual Connection here.
00114         if (actConn != null) {
00115             actConn.close();
00116         } else {
00117             logger.log(BasicLevel.ERROR, "Connection already closed");
00118         }
00119         actConn = null;
00120 
00121         // We can forget Connection and XAResource too.
00122         xaRes = null;
00123         implConn = null;
00124     }
00125 
00131     public void addConnectionEventListener(ConnectionEventListener listener) {
00132 
00133         logger.log(BasicLevel.DEBUG, "");
00134         eventListeners.addElement(listener);
00135     }
00136 
00142     public void removeConnectionEventListener(ConnectionEventListener listener) {
00143 
00144         logger.log(BasicLevel.DEBUG, "");
00145         eventListeners.removeElement(listener);
00146     }
00147 
00148     // -----------------------------------------------------------------
00149     // Other methods
00150     // -----------------------------------------------------------------
00151 
00152     /*
00153      * Notify a Close event on Connection
00154      */
00155     public void notifyClose() {
00156 
00157         logger.log(BasicLevel.DEBUG, "");
00158 
00159         // Notify event to listeners
00160         for (int i = 0; i < eventListeners.size(); i++) {
00161             ConnectionEventListener l = (ConnectionEventListener) eventListeners.elementAt(i);
00162             l.connectionClosed(new ConnectionEvent(this));
00163         }
00164     }
00165 
00166     /*
00167      * Notify an Error event on Connection
00168      */
00169     public void notifyError(SQLException ex) {
00170 
00171         logger.log(BasicLevel.DEBUG, "");
00172 
00173         // Notify event to listeners
00174         for (int i = 0; i < eventListeners.size(); i++) {
00175             ConnectionEventListener l = (ConnectionEventListener) eventListeners.elementAt(i);
00176             l.connectionErrorOccurred(new ConnectionEvent(this, ex));
00177         }
00178     }
00179 
00180 
00181 }

Generated on Tue Feb 15 15:05:16 2005 for JOnAS by  doxygen 1.3.9.1