Close the existing socket connection when reverting to the V2
authorKris Jurka <books@ejurka.com>
Tue, 22 Jun 2004 09:37:03 +0000 (09:37 +0000)
committerKris Jurka <books@ejurka.com>
Tue, 22 Jun 2004 09:37:03 +0000 (09:37 +0000)
protocol, or encountering other connection failures.

Laurent Sylvain

src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Connection.java

index ddee229d6e618e4d7bdfebb1ce4d46a20534d08a..19bd01aae59d9cd3aba417bb4233f8555812f638 100644 (file)
@@ -208,11 +208,21 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                        throw new PSQLException ("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e);
                }
 
-           //Now do the protocol work
-               if (haveMinimumCompatibleVersion("7.4")) {
-                       openConnectionV3(host,port,info,database,url,d,password);
-               } else {
-                       openConnectionV2(host,port,info,database,url,d,password);
+               try {
+                       //Now do the protocol work
+                       if (haveMinimumCompatibleVersion("7.4")) {
+                               openConnectionV3(host,port,info,database,url,d,password);
+                       } else {
+                               openConnectionV2(host,port,info,database,url,d,password);
+                       }
+               } catch (SQLException sqle) {
+                       // if we fail to completely establish a connection,
+                       // close down the socket to not leak resources.
+                       try {
+                               pgStream.close();
+                       } catch (IOException ioe) { }
+
+                       throw sqle;
                }
          }
 
@@ -305,6 +315,7 @@ public abstract class AbstractJdbc1Connection implements BaseConnection
                                                        //if the error length is > than 30000 we assume this is really a v2 protocol 
                                                        //server so try again with a v2 connection
                                                        //need to create a new connection and try again
+                                                       pgStream.close();
                                                        try
                                                        {
                                                                pgStream = new PGStream(p_host, p_port);