Index: src/interfaces/ecpg/ecpglib/connect.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/ecpg/ecpglib/connect.c,v
retrieving revision 1.19
diff -c -r1.19 connect.c
*** src/interfaces/ecpg/ecpglib/connect.c	29 Nov 2003 19:52:08 -0000	1.19
--- src/interfaces/ecpg/ecpglib/connect.c	7 Mar 2004 21:35:51 -0000
***************
*** 14,22 ****
  
  #ifdef ENABLE_THREAD_SAFETY
  static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
  #endif
  static struct connection *all_connections = NULL;
! static struct connection *actual_connection = NULL;
  
  static struct connection *
  ecpg_get_connection_nr(const char *connection_name)
--- 14,33 ----
  
  #ifdef ENABLE_THREAD_SAFETY
  static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
+ static pthread_key_t  actual_connection_key;
+ static pthread_once_t actual_connection_key_once = PTHREAD_ONCE_INIT;
+ #else
+ static struct connection *actual_connection = NULL;
  #endif
  static struct connection *all_connections = NULL;
! 
! #ifdef ENABLE_THREAD_SAFETY
! static void
! ecpg_actual_connection_init(void)
! {
!   pthread_key_create(&actual_connection_key, NULL);
! }
! #endif
  
  static struct connection *
  ecpg_get_connection_nr(const char *connection_name)
***************
*** 24,30 ****
--- 35,47 ----
  	struct connection *ret = NULL;
  
  	if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0))
+ 	{
+ #ifdef ENABLE_THREAD_SAFETY
+ 		ret = pthread_getspecific(actual_connection_key);
+ #else
  		ret = actual_connection;
+ #endif
+ 	}
  	else
  	{
  		struct connection *con;
***************
*** 86,93 ****
--- 103,115 ----
  				con->next = act->next;
  		}
  
+ #ifdef ENABLE_THREAD_SAFETY
+ 		if( pthread_getspecific(actual_connection_key) == act )
+ 		  pthread_setspecific(actual_connection_key, all_connections);
+ #else
  		if (actual_connection == act)
  			actual_connection = all_connections;
+ #endif
  
  		ECPGlog("ecpg_finish: Connection %s closed.\n", act->name);
  
***************
*** 150,156 ****
--- 172,182 ----
  	if (!ECPGinit(con, connection_name, lineno))
  		return (false);
  
+ #ifdef ENABLE_THREAD_SAFETY
+ 	pthread_setspecific(actual_connection_key, con);
+ #else
  	actual_connection = con;
+ #endif
  	return true;
  }
  
***************
*** 370,376 ****
  	else
  		this->next = all_connections;
  
! 	actual_connection = all_connections = this;
  
  	ECPGlog("ECPGconnect: opening database %s on %s port %s %s%s%s%s\n",
  			realname ? realname : "<DEFAULT>",
--- 396,408 ----
  	else
  		this->next = all_connections;
  
! 	all_connections = this;
! #ifdef ENABLE_THREAD_SAFETY
! 	pthread_once(&actual_connection_key_once, ecpg_actual_connection_init);
! 	pthread_setspecific(actual_connection_key, all_connections);
! #else
! 	actual_connection = all_connections;
! #endif
  
  	ECPGlog("ECPGconnect: opening database %s on %s port %s %s%s%s%s\n",
  			realname ? realname : "<DEFAULT>",
