Skip to content

Commit 9e29b32

Browse files
author
Dave Cramer
committed
patch to notify listeners on error from Csaba Nagy
1 parent f1792b9 commit 9e29b32

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

src/interfaces/jdbc/org/postgresql/jdbc2/optional/PooledConnectionImpl.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* @see ConnectionPool
1414
*
1515
* @author Aaron Mulder (ammulder@chariotsolutions.com)
16-
* @version $Revision: 1.6 $
16+
* @author Csaba Nagy (ncsaba@yahoo.com)
17+
* @version $Revision: 1.7 $
1718
*/
1819
public class PooledConnectionImpl implements PooledConnection
1920
{
@@ -95,33 +96,47 @@ public Connection getConnection() throws SQLException
9596
{
9697
if (con == null)
9798
{
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;
99103
}
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
102108
{
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)
105111
{
106-
try
112+
last.close();
113+
if (!con.getAutoCommit())
107114
{
108-
con.rollback();
115+
try
116+
{
117+
con.rollback();
118+
}
119+
catch (SQLException e)
120+
{}
109121
}
110-
catch (SQLException e)
111-
{}
122+
con.clearWarnings();
112123
}
113-
con.clearWarnings();
124+
con.setAutoCommit(autoCommit);
125+
}
126+
catch (SQLException sqlException)
127+
{
128+
fireConnectionFatalError(sqlException);
129+
throw (SQLException)sqlException.fillInStackTrace();
114130
}
115-
con.setAutoCommit(autoCommit);
116131
ConnectionHandler handler = new ConnectionHandler(con);
117132
last = handler;
118133
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;
121136
}
122137

123138
/**
124-
* Used to fire a connection event to all listeners.
139+
* Used to fire a connection closed event to all listeners.
125140
*/
126141
void fireConnectionClosed()
127142
{
@@ -140,7 +155,7 @@ void fireConnectionClosed()
140155
}
141156

142157
/**
143-
* Used to fire a connection event to all listeners.
158+
* Used to fire a connection error event to all listeners.
144159
*/
145160
void fireConnectionFatalError(SQLException e)
146161
{
@@ -363,7 +378,7 @@ else if (method.getName().equals("getConnection"))
363378
}
364379
else
365380
{
366-
try
381+
try
367382
{
368383
return method.invoke(st, args);
369384
}

0 commit comments

Comments
 (0)