[PATCH] PostgreSQL - close socket after failed connection attempt

Vladimir Sekissov <[email protected]> Thu, 22 Jun 2006 03:16:11 +0600 (YEKST)
Newsgroups gmane.lisp.clsql.devel
Message-ID <[email protected]>
Good day,

Reason for this patch:

PostgreSQL client code doesn't close connection implicitly when failed
to connect to database.

Patch was tested with PostgreSQL 7.4 and 8.1 under SBCL-0.9.13 and CLSQL-3.6.0

--- db-postgresql/postgresql-sql.lisp.orig   2005-09-18 06:13:12.000000000 +0600
+++ db-postgresql/postgresql-sql.lisp      2006-06-22 02:43:42.000000000 +0600
@@ -127,12 +127,14 @@
 	(declare (type pgsql-conn-def connection))
 	(when (not (eq (PQstatus connection) 
 		       pgsql-conn-status-type#connection-ok))
-	  (error 'sql-connection-error
-		 :database-type database-type
-		 :connection-spec connection-spec
-		 :error-id (PQstatus connection)
-		 :message (tidy-error-message 
-			   (PQerrorMessage connection))))
+          (let ((pqstatus (PQstatus connection))
+                (pqmessage (tidy-error-message (PQerrorMessage connection))))
+            (PQfinish connection)
+            (error 'sql-connection-error
+                   :database-type database-type
+                   :connection-spec connection-spec
+                   :error-id pqstatus
+                   :message  pqmessage)))
 	(make-instance 'postgresql-database
 		       :name (database-name-from-spec connection-spec
 						      database-type)

Best Regards,
Vladimir Sekissov