diff --git a/src/interfaces/ecpg/ChangeLog b/src/interfaces/ecpg/ChangeLog
index a5eb10b351ac800e8734979990e63bc3bed96b04..0572925d08d4abad4bac58e51190e439d6e144b8 100644
--- a/src/interfaces/ecpg/ChangeLog
+++ b/src/interfaces/ecpg/ChangeLog
@@ -2346,6 +2346,11 @@ Thu, 10 Apr 2008 12:42:25 +0200
 
 	- Fixed bug in PGTYPEStimestamp_sub that used pointers instead of the
 	  values to substract.
+
+Mon, 12 May 2008 18:19:08 +0200
+
+	- Check for non-existant connection in prepare statement handling.
+	- Do not close files that weren't opened.
 	- Set pgtypes library version to 3.1.
 	- Set compat library version to 3.1.
 	- Set ecpg library version to 6.2.
diff --git a/src/interfaces/ecpg/ecpglib/prepare.c b/src/interfaces/ecpg/ecpglib/prepare.c
index 6c0268bdb1d72fbda0548c6b8beb24783871d631..efe8e4c3f51e7dd5816440bc1c07d0a50ae247a6 100644
--- a/src/interfaces/ecpg/ecpglib/prepare.c
+++ b/src/interfaces/ecpg/ecpglib/prepare.c
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/prepare.c,v 1.26 2008/02/07 11:09:13 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/prepare.c,v 1.27 2008/05/12 16:29:04 meskes Exp $ */
 
 #define POSTGRES_ECPG_INTERNAL
 #include "postgres_fe.h"
@@ -120,10 +120,11 @@ ECPGprepare(int lineno, const char *connection_name, const int questionmarks, co
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 	PGresult   *query;
 
-	ecpg_init_sqlca(sqlca);
-
 	con = ecpg_get_connection(connection_name);
 
+	if (!ecpg_init(con, connection_name, lineno))
+		return false;
+
 	/* check if we already have prepared this statement */
 	this = find_prepared_statement(name, con, &prev);
 	if (this && !deallocate_one(lineno, ECPG_COMPAT_PGSQL, con, prev, this))
@@ -256,6 +257,9 @@ ECPGdeallocate(int lineno, int c, const char *connection_name, const char *name)
 
 	con = ecpg_get_connection(connection_name);
 
+	if (!ecpg_init(con, connection_name, lineno))
+		return false;
+
 	this = find_prepared_statement(name, con, &prev);
 	if (this)
 		return deallocate_one(lineno, c, con, prev, this);
diff --git a/src/interfaces/ecpg/preproc/preproc.y b/src/interfaces/ecpg/preproc/preproc.y
index fe351fdd271c7b163a979a5239c184dc0b9b5c98..e49ffc892b19e6f5ac402edf0c1904ffdb306c83 100644
--- a/src/interfaces/ecpg/preproc/preproc.y
+++ b/src/interfaces/ecpg/preproc/preproc.y
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/preproc.y,v 1.363 2008/03/27 07:56:00 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/preproc.y,v 1.364 2008/05/12 16:29:04 meskes Exp $ */
 
 /* Copyright comment */
 %{
@@ -87,8 +87,10 @@ mmerror(int error_code, enum errortype type, char * error, ...)
 			ret_value = error_code;
 			break;
 		case ET_FATAL:
-			fclose(yyin);
-			fclose(yyout);
+			if (yyin)
+				fclose(yyin);
+			if (yyout)
+				fclose(yyout);
 			if (unlink(output_filename) != 0 && *output_filename != '-')
 			        fprintf(stderr, "Could not remove output file %s!\n", output_filename);
 			exit(error_code);