diff -cprN head/src/interfaces/ecpg/ecpglib/execute.c work/src/interfaces/ecpg/ecpglib/execute.c
*** head/src/interfaces/ecpg/ecpglib/execute.c	Wed Jan  6 09:46:25 2010
--- work/src/interfaces/ecpg/ecpglib/execute.c	Tue Jan 19 13:39:40 2010
*************** ECPGdo(const int lineno, const int compa
*** 1753,1759 ****
--- 1753,1762 ----
  			stmt->command = ecpg_strdup(command, lineno);
  		}
  		else
+ 		{
  			ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, stmt->command);
+ 			return (false);
+ 		}
  	}
  
  	stmt->connection = con;
diff -cprN head/src/interfaces/ecpg/ecpglib/prepare.c work/src/interfaces/ecpg/ecpglib/prepare.c
*** head/src/interfaces/ecpg/ecpglib/prepare.c	Mon Jan 18 09:26:23 2010
--- work/src/interfaces/ecpg/ecpglib/prepare.c	Tue Jan 19 14:04:55 2010
*************** ecpg_freeStmtCacheEntry(int lineno, int 
*** 406,412 ****
   */
  static int
  AddStmtToCache(int lineno,		/* line # of statement		*/
! 			   char *stmtID,	/* statement ID				*/
  			   const char *connection,	/* connection				*/
  			   int compat,		/* compatibility level */
  			   const char *ecpgQuery)	/* query					*/
--- 406,412 ----
   */
  static int
  AddStmtToCache(int lineno,		/* line # of statement		*/
! 			   const char *stmtID,	/* statement ID				*/
  			   const char *connection,	/* connection				*/
  			   int compat,		/* compatibility level */
  			   const char *ecpgQuery)	/* query					*/
*************** ecpg_auto_prepare(int lineno, const char
*** 465,485 ****
  	/* if not found - add the statement to the cache	*/
  	if (entNo)
  	{
  		ecpg_log("ecpg_auto_prepare on line %d: statement found in cache; entry %d\n", lineno, entNo);
! 		*name = ecpg_strdup(stmtCacheEntries[entNo].stmtID, lineno);
  	}
  	else
  	{
  		ecpg_log("ecpg_auto_prepare on line %d: statement not in cache; inserting\n", lineno);
  
  		/* generate a statement ID */
! 		*name = (char *) ecpg_alloc(STMTID_SIZE, lineno);
! 		sprintf(*name, "ecpg%d", nextStmtID++);
  
! 		if (!ECPGprepare(lineno, connection_name, 0, ecpg_strdup(*name, lineno), query))
  			return (false);
! 		if (AddStmtToCache(lineno, *name, connection_name, compat, query) < 0)
  			return (false);
  	}
  
  	/* increase usage counter */
--- 465,503 ----
  	/* if not found - add the statement to the cache	*/
  	if (entNo)
  	{
+ 		struct connection *con;
+ 
  		ecpg_log("ecpg_auto_prepare on line %d: statement found in cache; entry %d\n", lineno, entNo);
! 		*name = stmtCacheEntries[entNo].stmtID;
! 
! 		if ((con = ecpg_get_connection(connection_name)) == NULL ||
! 			ecpg_find_prepared_statement(*name, con, NULL) == NULL)
! 		{
! 			/* The query is in cache, but not prepared on this connection. */
! 			if (!ECPGprepare(lineno, connection_name, 0, *name, query))
! 				return (false);
! 		}
! 
! 		*name = ecpg_strdup(*name, lineno);
  	}
  	else
  	{
+ 		char	stmtID[STMTID_SIZE];
+ 
  		ecpg_log("ecpg_auto_prepare on line %d: statement not in cache; inserting\n", lineno);
  
  		/* generate a statement ID */
! 		sprintf(stmtID, "ecpg%d", nextStmtID++);
  
! 		if (!ECPGprepare(lineno, connection_name, 0, stmtID, query))
  			return (false);
! 		if (AddStmtToCache(lineno, stmtID, connection_name, compat, query) < 0)
! 		{
! 			(void) ECPGdeallocate(lineno, compat, connection_name, stmtID);
  			return (false);
+ 		}
+ 
+ 		*name = ecpg_strdup(stmtID, lineno);
  	}
  
  	/* increase usage counter */
