source: Ballon/out/artifacts/geisa_artifact/WEB-INF/lib/mysql-connector-java-5.1.21/src/com/mysql/jdbc/jdbc2/optional/MysqlPooledConnection.java @ 848

Last change on this file since 848 was 766, checked in by npipsl, 11 years ago
File size: 7.7 KB
Line 
1/*
2 Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4
5  The MySQL Connector/J is licensed under the terms of the GPLv2
6  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
7  There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
8  this software, see the FLOSS License Exception
9  <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
10
11  This program is free software; you can redistribute it and/or modify it under the terms
12  of the GNU General Public License as published by the Free Software Foundation; version 2
13  of the License.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
16  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  See the GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License along with this
20  program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
21  Floor, Boston, MA 02110-1301  USA
22
23 */
24
25package com.mysql.jdbc.jdbc2.optional;
26
27import java.lang.reflect.Constructor;
28import java.sql.Connection;
29import java.sql.SQLException;
30import java.util.HashMap;
31import java.util.Iterator;
32import java.util.Map;
33
34import javax.sql.ConnectionEvent;
35import javax.sql.ConnectionEventListener;
36import javax.sql.PooledConnection;
37
38import com.mysql.jdbc.ExceptionInterceptor;
39import com.mysql.jdbc.SQLError;
40import com.mysql.jdbc.Util;
41
42/**
43 * This class is used to wrap and return a physical connection within a logical
44 * handle. It also registers and notifies ConnectionEventListeners of any
45 * ConnectionEvents
46 *
47 * @see javax.sql.PooledConnection
48 * @see org.gjt.mm.mysql.jdbc2.optional.LogicalHandle
49 * @author Todd Wolff <todd.wolff_at_prodigy.net>
50 */
51public class MysqlPooledConnection implements PooledConnection {
52
53        private static final Constructor<?> JDBC_4_POOLED_CONNECTION_WRAPPER_CTOR;
54
55        static {
56                if (Util.isJdbc4()) {
57                        try {
58                                JDBC_4_POOLED_CONNECTION_WRAPPER_CTOR = Class.forName(
59                                                "com.mysql.jdbc.jdbc2.optional.JDBC4MysqlPooledConnection")
60                                                .getConstructor(
61                                                                new Class[] { com.mysql.jdbc.Connection.class });
62                        } catch (SecurityException e) {
63                                throw new RuntimeException(e);
64                        } catch (NoSuchMethodException e) {
65                                throw new RuntimeException(e);
66                        } catch (ClassNotFoundException e) {
67                                throw new RuntimeException(e);
68                        }
69                } else {
70                        JDBC_4_POOLED_CONNECTION_WRAPPER_CTOR = null;
71                }
72        }
73
74        protected static MysqlPooledConnection getInstance(com.mysql.jdbc.Connection connection) throws SQLException {
75                if (!Util.isJdbc4()) {
76                        return new MysqlPooledConnection(connection);
77                }
78
79                return (MysqlPooledConnection) Util.handleNewInstance(
80                                JDBC_4_POOLED_CONNECTION_WRAPPER_CTOR, new Object[] {
81                                                connection}, connection.getExceptionInterceptor());
82        }
83       
84        /**
85         * The flag for an exception being thrown.
86         */
87        public static final int CONNECTION_ERROR_EVENT = 1;
88
89        /**
90         * The flag for a connection being closed.
91         */
92        public static final int CONNECTION_CLOSED_EVENT = 2;
93
94        // ~ Instance/static variables .............................................
95
96        private Map<ConnectionEventListener, ConnectionEventListener> connectionEventListeners;
97
98        private Connection logicalHandle;
99
100        private com.mysql.jdbc.Connection physicalConn;
101       
102        private ExceptionInterceptor exceptionInterceptor;
103
104        // ~ Constructors ..........................................................
105
106        /**
107         * Construct a new MysqlPooledConnection and set instance variables
108         *
109         * @param connection
110         *            physical connection to db
111         */
112        public MysqlPooledConnection(com.mysql.jdbc.Connection connection) {
113                this.logicalHandle = null;
114                this.physicalConn = connection;
115                this.connectionEventListeners = new HashMap<ConnectionEventListener, ConnectionEventListener>();
116                this.exceptionInterceptor = this.physicalConn.getExceptionInterceptor();
117        }
118
119        /**
120         * Adds ConnectionEventListeners to a hash table to be used for notification
121         * of ConnectionEvents
122         *
123         * @param connectioneventlistener
124         *            listener to be notified with ConnectionEvents
125         */
126        public synchronized void addConnectionEventListener(
127                        ConnectionEventListener connectioneventlistener) {
128
129                if (this.connectionEventListeners != null) {
130                        this.connectionEventListeners.put(connectioneventlistener,
131                                        connectioneventlistener);
132                }
133        }
134
135        /**
136         * Removes ConnectionEventListeners from hash table used for notification of
137         * ConnectionEvents
138         *
139         * @param connectioneventlistener
140         *            listener to be removed
141         */
142        public synchronized void removeConnectionEventListener(
143                        ConnectionEventListener connectioneventlistener) {
144
145                if (this.connectionEventListeners != null) {
146                        this.connectionEventListeners.remove(connectioneventlistener);
147                }
148        }
149
150        /**
151         * Invoked by the container. Return a logicalHandle object that wraps a
152         * physical connection.
153         *
154         * @see java.sql.DataSource#getConnection()
155         */
156        public synchronized Connection getConnection() throws SQLException {
157                return getConnection(true, false);
158               
159        }
160       
161        protected synchronized Connection getConnection(boolean resetServerState, 
162                        boolean forXa)
163                throws SQLException {
164                if (this.physicalConn == null) {
165
166                        SQLException sqlException = SQLError.createSQLException(
167                                        "Physical Connection doesn't exist", this.exceptionInterceptor);
168                        callConnectionEventListeners(CONNECTION_ERROR_EVENT, sqlException);
169
170                        throw sqlException;
171                }
172
173                try {
174
175                        if (this.logicalHandle != null) {
176                                ((ConnectionWrapper) this.logicalHandle).close(false);
177                        }
178
179                        if (resetServerState) {
180                                this.physicalConn.resetServerState();
181                        }
182
183                        this.logicalHandle = ConnectionWrapper.getInstance(this, 
184                                        this.physicalConn, 
185                                        forXa);
186                } catch (SQLException sqlException) {
187                        callConnectionEventListeners(CONNECTION_ERROR_EVENT, sqlException);
188
189                        throw sqlException;
190                }
191
192                return this.logicalHandle;
193        }
194
195        /**
196         * Invoked by the container (not the client), and should close the physical
197         * connection. This will be called if the pool is destroyed or the
198         * connectionEventListener receives a connectionErrorOccurred event.
199         *
200         * @see java.sql.DataSource#close()
201         */
202        public synchronized void close() throws SQLException {
203                if (this.physicalConn != null) {
204                        this.physicalConn.close();
205                       
206                        this.physicalConn = null;
207                }
208               
209                if (this.connectionEventListeners != null) {
210                        this.connectionEventListeners.clear();
211                       
212                        this.connectionEventListeners = null;
213                }
214        }
215
216        /**
217         * Notifies all registered ConnectionEventListeners of ConnectionEvents.
218         * Instantiates a new ConnectionEvent which wraps sqlException and invokes
219         * either connectionClose or connectionErrorOccurred on listener as
220         * appropriate.
221         *
222         * @param eventType
223         *            value indicating whether connectionClosed or
224         *            connectionErrorOccurred called
225         * @param sqlException
226         *            the exception being thrown
227         */
228        protected synchronized void callConnectionEventListeners(int eventType,
229                        SQLException sqlException) {
230
231                if (this.connectionEventListeners == null) {
232
233                        return;
234                }
235
236                Iterator<Map.Entry<ConnectionEventListener, ConnectionEventListener>> iterator = this.connectionEventListeners.entrySet().iterator();
237               
238                ConnectionEvent connectionevent = new ConnectionEvent(this,
239                                sqlException);
240
241                while (iterator.hasNext()) {
242
243                        ConnectionEventListener connectioneventlistener = iterator.next().getValue();
244
245                        if (eventType == CONNECTION_CLOSED_EVENT) {
246                                connectioneventlistener.connectionClosed(connectionevent);
247                        } else if (eventType == CONNECTION_ERROR_EVENT) {
248                                connectioneventlistener
249                                                .connectionErrorOccurred(connectionevent);
250                        }
251                }
252        }
253       
254        protected ExceptionInterceptor getExceptionInterceptor() {
255                return this.exceptionInterceptor;
256        }
257}
Note: See TracBrowser for help on using the repository browser.