13
13
* @see ConnectionPool
14
14
*
15
15
* @author Aaron Mulder (ammulder@chariotsolutions.com)
16
- * @version $Revision: 1.6 $
16
+ * @author Csaba Nagy (ncsaba@yahoo.com)
17
+ * @version $Revision: 1.7 $
17
18
*/
18
19
public class PooledConnectionImpl implements PooledConnection
19
20
{
@@ -95,33 +96,47 @@ public Connection getConnection() throws SQLException
95
96
{
96
97
if (con ==null )
97
98
{
98
- throw new SQLException ("This PooledConnection has already been closed!" );
99
+ // Before throwing the exception, let's notify the registered listeners about the error
100
+ final SQLException sqlException =new SQLException ("This PooledConnection has already been closed!" );
101
+ fireConnectionFatalError (sqlException );
102
+ throw sqlException ;
99
103
}
100
- // Only one connection can be open at a time from this PooledConnection. See JDBC 2.0 Optional Package spec section 6.2.3
101
- if (last !=null )
104
+ // If any error occures while opening a new connection, the listeners
105
+ // have to be notified. This gives a chance to connection pools to
106
+ // elliminate bad pooled connections.
107
+ try
102
108
{
103
- last . close ();
104
- if (! con . getAutoCommit () )
109
+ // Only one connection can be open at a time from this PooledConnection. See JDBC 2.0 Optional Package spec section 6.2.3
110
+ if (last != null )
105
111
{
106
- try
112
+ last .close ();
113
+ if (!con .getAutoCommit ())
107
114
{
108
- con .rollback ();
115
+ try
116
+ {
117
+ con .rollback ();
118
+ }
119
+ catch (SQLException e )
120
+ {}
109
121
}
110
- catch (SQLException e )
111
- {}
122
+ con .clearWarnings ();
112
123
}
113
- con .clearWarnings ();
124
+ con .setAutoCommit (autoCommit );
125
+ }
126
+ catch (SQLException sqlException )
127
+ {
128
+ fireConnectionFatalError (sqlException );
129
+ throw (SQLException )sqlException .fillInStackTrace ();
114
130
}
115
- con .setAutoCommit (autoCommit );
116
131
ConnectionHandler handler =new ConnectionHandler (con );
117
132
last =handler ;
118
133
Connection con = (Connection )Proxy .newProxyInstance (getClass ().getClassLoader (),new Class []{Connection .class ,PGConnection .class },handler );
119
- last .setProxy (con );
120
- return con ;
134
+ last .setProxy (con );
135
+ return con ;
121
136
}
122
137
123
138
/**
124
- * Used to fire a connection event to all listeners.
139
+ * Used to fire a connectionclosed event to all listeners.
125
140
*/
126
141
void fireConnectionClosed ()
127
142
{
@@ -140,7 +155,7 @@ void fireConnectionClosed()
140
155
}
141
156
142
157
/**
143
- * Used to fire a connection event to all listeners.
158
+ * Used to fire a connectionerror event to all listeners.
144
159
*/
145
160
void fireConnectionFatalError (SQLException e )
146
161
{
@@ -363,7 +378,7 @@ else if (method.getName().equals("getConnection"))
363
378
}
364
379
else
365
380
{
366
- try
381
+ try
367
382
{
368
383
return method .invoke (st ,args );
369
384
}