--- /home/cnagy/dev/src/pgsql-7_4_STABLE/src/interfaces/jdbc/org/postgresql/jdbc2/optional/PooledConnectionImpl.java	2004-02-24 14:11:44.000000000 +0100
+++ src/share/org/postgresql/jdbc2/optional/PooledConnectionImpl.java	2005-06-27 13:34:33.000000000 +0200
@@ -5,6 +5,8 @@
 import java.util.*;
 import java.lang.reflect.*;
 import org.postgresql.PGConnection;
+import org.postgresql.util.PSQLState;
+import org.postgresql.util.PSQLException;
 
 /**
  * PostgreSQL implementation of the PooledConnection interface.  This shouldn't
@@ -14,7 +16,7 @@
  *
  * @author Aaron Mulder (ammulder@chariotsolutions.com)
  * @author Csaba Nagy (ncsaba@yahoo.com)
- * @version $Revision: 1.7.4.3 $
+ * @version $Revision: 1.1.2.2 $
  */
 public class PooledConnectionImpl implements PooledConnection
 {
@@ -97,7 +99,8 @@
 		if (con == null)
 		{
 			// Before throwing the exception, let's notify the registered listeners about the error
-			final SQLException sqlException = new SQLException("This PooledConnection has already been closed!");
+            PSQLException sqlException = new PSQLException("This PooledConnection has already been closed.",
+                                                           PSQLState.CONNECTION_DOES_NOT_EXIST);
 			fireConnectionFatalError(sqlException);
 			throw sqlException;
 		}
@@ -173,6 +176,50 @@
 		}
 	}
 
+    // 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
@@ -236,7 +283,8 @@
 			}
 			if (con == null && !method.getName().equals("close"))
 			{
-				throw new SQLException(automatic ? "Connection has been closed automatically because a new connection was opened for the same PooledConnection or the PooledConnection has been closed" : "Connection has been closed");
+                throw new PSQLException(automatic ? "Connection has been closed automatically because a new connection was opened for the same PooledConnection or the PooledConnection has been closed." : "Connection has been closed.",
+                                        PSQLState.CONNECTION_DOES_NOT_EXIST);
 			}
 			if (method.getName().equals("close"))
 			{
@@ -268,17 +316,22 @@
 				}
 				return null;
 			}
-            else if(method.getName().equals("createStatement"))
+
+            // 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"))
+                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"))
+                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));
@@ -287,6 +340,12 @@
 			{
 				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() {
@@ -323,11 +382,11 @@
      * The StatementHandler is required in order to return the proper
      * Connection proxy for the getConnection method.
      */
-    private static class StatementHandler implements InvocationHandler {
-        private ConnectionHandler con;
+    private class StatementHandler implements InvocationHandler {
+        private PooledConnectionImpl.ConnectionHandler con;
         private Statement st;
 
-        public StatementHandler(ConnectionHandler con, Statement st) {
+        public StatementHandler(PooledConnectionImpl.ConnectionHandler con, Statement st) {
             this.con = con;
             this.st = st;
         }
@@ -379,22 +438,23 @@
             }
             if (st == null || con.isClosed())
             {
-                throw new SQLException("Statement has been closed");
+                throw new PSQLException("Statement has been closed.",
+                                        PSQLState.UNKNOWN_STATE);
             }
-            else if (method.getName().equals("getConnection"))
+            
+            if (method.getName().equals("getConnection"))
             {
                 return con.getProxy(); // the proxied connection, not a physical connection
             }
-            else
-            {
+
                 try
                 {
                     return method.invoke(st, args);
-                }
-                catch (InvocationTargetException e)
-                {
-                    throw e.getTargetException();
-                }
+            } catch (InvocationTargetException e) {
+                Throwable te = e.getTargetException();
+                if (te instanceof SQLException)
+                    fireConnectionError((SQLException)te); // Tell listeners about exception if it's fatal
+                throw te;
             }
         }
     }
