File: AbstractJdbc23PooledConnection.java

package info (click to toggle)
libpgjava 8.4-701-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 4,162
  • sloc: java: 33,948; xml: 3,158; makefile: 14; sh: 10
file content (464 lines) | stat: -rw-r--r-- 16,420 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/ds/jdbc23/AbstractJdbc23PooledConnection.java,v 1.6 2009/01/15 04:18:03 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.ds.jdbc23;

import javax.sql.*;
import java.sql.*;
import java.util.*;
import java.lang.reflect.*;
import org.postgresql.PGConnection;
import org.postgresql.util.GT;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;

/**
 * PostgreSQL implementation of the PooledConnection interface.  This shouldn't
 * be used directly, as the pooling client should just interact with the
 * ConnectionPool instead.
 * @see org.postgresql.ds.PGConnectionPoolDataSource
 *
 * @author Aaron Mulder (ammulder@chariotsolutions.com)
 * @author Csaba Nagy (ncsaba@yahoo.com)
 */
public abstract class AbstractJdbc23PooledConnection
{
    private List listeners = new LinkedList();
    private Connection con;
    private ConnectionHandler last;
    private final boolean autoCommit;
    private final boolean isXA;

    /**
     * Creates a new PooledConnection representing the specified physical
     * connection.
     */
    public AbstractJdbc23PooledConnection(Connection con, boolean autoCommit, boolean isXA)
    {
        this.con = con;
        this.autoCommit = autoCommit;
        this.isXA = isXA;
    }

    /**
     * Adds a listener for close or fatal error events on the connection
     * handed out to a client.
     */
    public void addConnectionEventListener(ConnectionEventListener connectionEventListener)
    {
        listeners.add(connectionEventListener);
    }

    /**
     * Removes a listener for close or fatal error events on the connection
     * handed out to a client.
     */
    public void removeConnectionEventListener(ConnectionEventListener connectionEventListener)
    {
        listeners.remove(connectionEventListener);
    }

    /**
     * Closes the physical database connection represented by this
     * PooledConnection.  If any client has a connection based on
     * this PooledConnection, it is forcibly closed as well.
     */
    public void close() throws SQLException
    {
        if (last != null)
        {
            last.close();
            if (!con.getAutoCommit())
            {
                try
                {
                    con.rollback();
                }
                catch (SQLException e)
                {
                }
            }
        }
        try
        {
            con.close();
        }
        finally
        {
            con = null;
        }
    }

    /**
     * Gets a handle for a client to use.  This is a wrapper around the
     * physical connection, so the client can call close and it will just
     * return the connection to the pool without really closing the
     * pgysical connection.
     *
     * <p>According to the JDBC 2.0 Optional Package spec (6.2.3), only one
     * client may have an active handle to the connection at a time, so if
     * there is a previous handle active when this is called, the previous
     * one is forcibly closed and its work rolled back.</p>
     */
    public Connection getConnection() throws SQLException
    {
        if (con == null)
        {
            // Before throwing the exception, let's notify the registered listeners about the error
            PSQLException sqlException = new PSQLException(GT.tr("This PooledConnection has already been closed."),
                                                           PSQLState.CONNECTION_DOES_NOT_EXIST);
            fireConnectionFatalError(sqlException);
            throw sqlException;
        }
        // If any error occures while opening a new connection, the listeners
        // have to be notified. This gives a chance to connection pools to
        // eliminate bad pooled connections.
        try
        {
            // Only one connection can be open at a time from this PooledConnection.  See JDBC 2.0 Optional Package spec section 6.2.3
            if (last != null)
            {
                last.close();
                if (!con.getAutoCommit())
                {
                    try
                    {
                        con.rollback();
                    }
                    catch (SQLException e)
                    {
                    }
                }
                con.clearWarnings();
            }
            /*
             * In XA-mode, autocommit is handled in PGXAConnection,
             * because it depends on whether an XA-transaction is open
             * or not
             */
            if (!isXA)
                con.setAutoCommit(autoCommit);
        }
        catch (SQLException sqlException)
        {
            fireConnectionFatalError(sqlException);
            throw (SQLException)sqlException.fillInStackTrace();
        }
        ConnectionHandler handler = new ConnectionHandler(con);
        last = handler;

        Connection proxyCon = (Connection)Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{Connection.class, PGConnection.class}, handler);
        last.setProxy(proxyCon);
        return proxyCon;
    }

    /**
     * Used to fire a connection closed event to all listeners.
     */
    void fireConnectionClosed()
    {
        ConnectionEvent evt = null;
        // Copy the listener list so the listener can remove itself during this method call
        ConnectionEventListener[] local = (ConnectionEventListener[]) listeners.toArray(new ConnectionEventListener[listeners.size()]);
        for (int i = 0; i < local.length; i++)
        {
            ConnectionEventListener listener = local[i];
            if (evt == null)
            {
                evt = createConnectionEvent(null);
            }
            listener.connectionClosed(evt);
        }
    }

    /**
     * Used to fire a connection error event to all listeners.
     */
    void fireConnectionFatalError(SQLException e)
    {
        ConnectionEvent evt = null;
        // Copy the listener list so the listener can remove itself during this method call
        ConnectionEventListener[] local = (ConnectionEventListener[])listeners.toArray(new ConnectionEventListener[listeners.size()]);
        for (int i = 0; i < local.length; i++)
        {
            ConnectionEventListener listener = local[i];
            if (evt == null)
            {
                evt = createConnectionEvent(e);
            }
            listener.connectionErrorOccurred(evt);
        }
    }

    protected abstract ConnectionEvent createConnectionEvent(SQLException e);

    // Classes we consider fatal.
    private static String[] fatalClasses = {
        "08",  // connection error
        "53",  // insufficient resources

        // nb: not just "57" as that includes query cancel which is nonfatal
        "57P01",  // admin shutdown
        "57P02",  // crash shutdown
        "57P03",  // cannot connect now

        "58",  // system error (backend)
        "60",  // system error (driver)
        "99",  // unexpected error
        "F0",  // configuration file error (backend)
        "XX",  // internal error (backend)
    };

    private static boolean isFatalState(String state) {
        if (state == null)      // no info, assume fatal
            return true;
        if (state.length() < 2) // no class info, assume fatal
            return true;

        for (int i = 0; i < fatalClasses.length; ++i)
            if (state.startsWith(fatalClasses[i]))
                return true; // fatal

        return false;
    }

    /**
     * Fires a connection error event, but only if we
     * think the exception is fatal.
     *
     * @param e the SQLException to consider
     */    
    private void fireConnectionError(SQLException e) 
    {
        if (!isFatalState(e.getSQLState()))
            return;

        fireConnectionFatalError(e);
    }

    /**
     * Instead of declaring a class implementing Connection, which would have
     * to be updated for every JDK rev, use a dynamic proxy to handle all
     * calls through the Connection interface. This is the part that
     * requires JDK 1.3 or higher, though JDK 1.2 could be supported with a
     * 3rd-party proxy package.
     */
    private class ConnectionHandler implements InvocationHandler
    {
        private Connection con;
        private Connection proxy; // the Connection the client is currently using, which is a proxy
        private boolean automatic = false;

        public ConnectionHandler(Connection con)
        {
            this.con = con;
        }

        public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable
        {
            // From Object
            if (method.getDeclaringClass().getName().equals("java.lang.Object"))
            {
                if (method.getName().equals("toString"))
                {
                    return "Pooled connection wrapping physical connection " + con;
                }
                if (method.getName().equals("equals"))
                {
                    return new Boolean(proxy == args[0]);
                }
                if (method.getName().equals("hashCode"))
                {
                    return new Integer(System.identityHashCode(proxy));
                }
                try
                {
                    return method.invoke(con, args);
                }
                catch (InvocationTargetException e)
                {
                    throw e.getTargetException();
                }
            }
            // All the rest is from the Connection or PGConnection interface
            if (method.getName().equals("isClosed"))
            {
                return con == null ? Boolean.TRUE : Boolean.FALSE;
            }
            if (con == null && !method.getName().equals("close"))
            {
                throw new PSQLException(automatic ? GT.tr("Connection has been closed automatically because a new connection was opened for the same PooledConnection or the PooledConnection has been closed.") : GT.tr("Connection has been closed."),
                                        PSQLState.CONNECTION_DOES_NOT_EXIST);
            }
            if (method.getName().equals("close"))
            {
                // we are already closed and a double close
                // is not an error.
                if (con == null)
                    return null;

                SQLException ex = null;
                if (!isXA && !con.getAutoCommit())
                {
                    try
                    {
                        con.rollback();
                    }
                    catch (SQLException e)
                    {
                        ex = e;
                    }
                }
                con.clearWarnings();
                con = null;
                this.proxy = null;
                last = null;
                fireConnectionClosed();
                if (ex != null)
                {
                    throw ex;
                }
                return null;
            }
            
            // From here on in, we invoke via reflection, catch exceptions,
            // and check if they're fatal before rethrowing.

            try {            
                if (method.getName().equals("createStatement"))
                {
                    Statement st = (Statement)method.invoke(con, args);
                    return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{Statement.class, org.postgresql.PGStatement.class}, new StatementHandler(this, st));
                }
                else if (method.getName().equals("prepareCall"))
                {
                    Statement st = (Statement)method.invoke(con, args);
                    return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{CallableStatement.class, org.postgresql.PGStatement.class}, new StatementHandler(this, st));
                }
                else if (method.getName().equals("prepareStatement"))
                {
                    Statement st = (Statement)method.invoke(con, args);
                    return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{PreparedStatement.class, org.postgresql.PGStatement.class}, new StatementHandler(this, st));
                }
                else
                {
                    return method.invoke(con, args);
                }
            } catch (InvocationTargetException e) {
                Throwable te = e.getTargetException();
                if (te instanceof SQLException)
                    fireConnectionError((SQLException)te); // Tell listeners about exception if it's fatal
                throw te;
            }
        }

        Connection getProxy() {
            return proxy;
        }

        void setProxy(Connection proxy) {
            this.proxy = proxy;
        }

        public void close()
        {
            if (con != null)
            {
                automatic = true;
            }
            con = null;
            proxy = null;
            // No close event fired here: see JDBC 2.0 Optional Package spec section 6.3
        }

        public boolean isClosed() {
            return con == null;
        }
    }

    /**
     * Instead of declaring classes implementing Statement, PreparedStatement,
     * and CallableStatement, which would have to be updated for every JDK rev,
     * use a dynamic proxy to handle all calls through the Statement
     * interfaces. This is the part that requires JDK 1.3 or higher, though
     * JDK 1.2 could be supported with a 3rd-party proxy package.
     *
     * The StatementHandler is required in order to return the proper
     * Connection proxy for the getConnection method.
     */
    private class StatementHandler implements InvocationHandler {
        private AbstractJdbc23PooledConnection.ConnectionHandler con;
        private Statement st;

        public StatementHandler(AbstractJdbc23PooledConnection.ConnectionHandler con, Statement st) {
            this.con = con;
            this.st = st;
        }
        public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable
        {
            // From Object
            if (method.getDeclaringClass().getName().equals("java.lang.Object"))
            {
                if (method.getName().equals("toString"))
                {
                    return "Pooled statement wrapping physical statement " + st;
                }
                if (method.getName().equals("hashCode"))
                {
                    return new Integer(System.identityHashCode(proxy));
                }
                if (method.getName().equals("equals"))
                {
                    return new Boolean(proxy == args[0]);
                }
                return method.invoke(st, args);
            }
            // All the rest is from the Statement interface
            if (method.getName().equals("close"))
            {
                // closing an already closed object is a no-op
                if (st == null || con.isClosed())
                    return null;

                try
                {
                    st.close();
                }
                finally
                {
                    con = null;
                    st = null;
                }
                return null;
            }
            if (st == null || con.isClosed())
            {
                throw new PSQLException(GT.tr("Statement has been closed."),
                                        PSQLState.OBJECT_NOT_IN_STATE);
            }
            
            if (method.getName().equals("getConnection"))
            {
                return con.getProxy(); // the proxied connection, not a physical connection
            }

            try
            {
                return method.invoke(st, args);
            } catch (InvocationTargetException e) {
                Throwable te = e.getTargetException();
                if (te instanceof SQLException)
                    fireConnectionError((SQLException)te); // Tell listeners about exception if it's fatal
                throw te;
            }
        }
    }
}