~ubuntu-branches/ubuntu/karmic/mysql-connector-java/karmic

« back to all changes in this revision

Viewing changes to com/mysql/jdbc/jdbc2/optional/MysqlPooledConnection.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2003-12-30 22:37:53 UTC
  • Revision ID: james.westby@ubuntu.com-20031230223753-5d9n2b0w2ms6puqa
Tags: upstream-3.0.9
ImportĀ upstreamĀ versionĀ 3.0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2002 MySQL AB
 
3
   
 
4
      This program is free software; you can redistribute it and/or modify
 
5
      it under the terms of the GNU General Public License as published by
 
6
      the Free Software Foundation; either version 2 of the License, or
 
7
      (at your option) any later version.
 
8
   
 
9
      This program is distributed in the hope that it will be useful,
 
10
      but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
      GNU General Public License for more details.
 
13
   
 
14
      You should have received a copy of the GNU General Public License
 
15
      along with this program; if not, write to the Free Software
 
16
      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
      
 
18
 */
 
19
package com.mysql.jdbc.jdbc2.optional;
 
20
 
 
21
import java.sql.Connection;
 
22
import java.sql.SQLException;
 
23
 
 
24
import java.util.Enumeration;
 
25
import java.util.Hashtable;
 
26
 
 
27
import javax.sql.ConnectionEvent;
 
28
import javax.sql.ConnectionEventListener;
 
29
import javax.sql.PooledConnection;
 
30
 
 
31
 
 
32
/**
 
33
 * This class is used to wrap and return a physical connection within a logical handle.
 
34
 * It also registers and notifies ConnectionEventListeners of any ConnectionEvents
 
35
 *
 
36
 * @see javax.sql.PooledConnection
 
37
 * @see org.gjt.mm.mysql.jdbc2.optional.LogicalHandle
 
38
 * @author Todd Wolff <todd.wolff_at_prodigy.net>
 
39
 */
 
40
public class MysqlPooledConnection
 
41
    implements PooledConnection {
 
42
        
 
43
        /**
 
44
         * The flag for an exception being thrown.
 
45
         */
 
46
        public static final int CONNECTION_ERROR_EVENT = 1;
 
47
        
 
48
        /**
 
49
         * The flag for a connection being closed.
 
50
         */
 
51
        public static final int CONNECTION_CLOSED_EVENT = 2;
 
52
        
 
53
    //~ Instance/static variables .............................................
 
54
 
 
55
    private Hashtable eventListeners;
 
56
    private Connection logicalHandle;
 
57
    private Connection physicalConn;
 
58
    
 
59
 
 
60
    //~ Constructors ..........................................................
 
61
 
 
62
    /**
 
63
    * Construct a new MysqlPooledConnection and set instance variables
 
64
    *
 
65
    * @param connection physical connection to db
 
66
    */
 
67
    public MysqlPooledConnection(Connection connection) {
 
68
        logicalHandle = null;
 
69
        physicalConn = connection;
 
70
        eventListeners = new Hashtable(10);
 
71
    }
 
72
 
 
73
    //~ Methods ...............................................................
 
74
 
 
75
    /**
 
76
     * Adds ConnectionEventListeners to a hash table to be used for notification of
 
77
     * ConnectionEvents
 
78
     *
 
79
     * @param connectioneventlistener listener to be notified with ConnectionEvents
 
80
     */
 
81
    public synchronized void addConnectionEventListener(ConnectionEventListener connectioneventlistener) {
 
82
 
 
83
        if (eventListeners != null) {
 
84
            eventListeners.put(connectioneventlistener, 
 
85
                               connectioneventlistener);
 
86
        }
 
87
    }
 
88
 
 
89
    /**
 
90
     * Removes ConnectionEventListeners from hash table used for notification of
 
91
     * ConnectionEvents
 
92
     *
 
93
     * @param connectioneventlistener listener to be removed
 
94
     */
 
95
    public synchronized void removeConnectionEventListener(ConnectionEventListener connectioneventlistener) {
 
96
 
 
97
        if (eventListeners != null) {
 
98
            eventListeners.remove(connectioneventlistener);
 
99
        }
 
100
    }
 
101
 
 
102
    /**
 
103
     * Invoked by the container.  Return a logicalHandle object that wraps a physical 
 
104
     * connection.
 
105
     *
 
106
     * @see java.sql.DataSource#getConnection()
 
107
     */
 
108
    public synchronized Connection getConnection()
 
109
                                          throws SQLException {
 
110
 
 
111
        if (physicalConn == null) {
 
112
 
 
113
            SQLException sqlException = new SQLException(
 
114
                                                "Physical Connection doesn't exist");
 
115
            callListener(CONNECTION_ERROR_EVENT, sqlException);
 
116
 
 
117
            return null;
 
118
        }
 
119
 
 
120
        try {
 
121
 
 
122
            if (logicalHandle != null) {
 
123
                ((LogicalHandle)logicalHandle).close(false);
 
124
            }
 
125
 
 
126
            logicalHandle = new LogicalHandle(this, physicalConn);
 
127
        } catch (SQLException sqlException) {
 
128
            callListener(CONNECTION_ERROR_EVENT, sqlException);
 
129
 
 
130
            return null;
 
131
        }
 
132
 
 
133
        return logicalHandle;
 
134
    }
 
135
 
 
136
    /**
 
137
     * Invoked by the container (not the client), and should close the physical connection.
 
138
     * This will be called if the pool is destroyed or the connectionEventListener receives
 
139
     * a connectionErrorOccurred event.
 
140
     *
 
141
     * @see java.sql.DataSource#close()
 
142
     */
 
143
    public synchronized void close()
 
144
                            throws SQLException {
 
145
        physicalConn.close();
 
146
        physicalConn = null;
 
147
    }
 
148
 
 
149
    /**
 
150
     * Notifies all registered ConnectionEventListeners of ConnectionEvents.  Instantiates
 
151
     * a new ConnectionEvent which wraps sqlException and invokes either connectionClose
 
152
     * or connectionErrorOccurred on listener as appropriate.
 
153
     * 
 
154
     * @param eventType value indicating whether connectionClosed or connectionErrorOccurred called
 
155
     * @param sqlException the exception being thrown
 
156
     */
 
157
    protected synchronized void callListener(int eventType, SQLException sqlException) {
 
158
 
 
159
        if (eventListeners == null) {
 
160
 
 
161
            return;
 
162
        }
 
163
 
 
164
        Enumeration enumeration = eventListeners.keys();
 
165
        ConnectionEvent connectionevent = new ConnectionEvent(this, 
 
166
                                                              sqlException);
 
167
 
 
168
        while (enumeration.hasMoreElements()) {
 
169
 
 
170
            ConnectionEventListener connectioneventlistener = 
 
171
                    (ConnectionEventListener) enumeration.nextElement();
 
172
            ConnectionEventListener connectioneventlistener1 = 
 
173
                    (ConnectionEventListener) eventListeners.get(
 
174
                            connectioneventlistener);
 
175
 
 
176
            if (eventType == CONNECTION_CLOSED_EVENT) {
 
177
                connectioneventlistener1.connectionClosed(connectionevent);
 
178
            } else if (eventType == CONNECTION_ERROR_EVENT) {
 
179
                connectioneventlistener1.connectionErrorOccurred(
 
180
                        connectionevent);
 
181
            }
 
182
        }
 
183
    }
 
184
}