1- /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.24 2004/12/30 09:36:37 meskes Exp $ */
1+ /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.25 2005/04/14 10:08:57 meskes Exp $ */
22
33#define POSTGRES_ECPG_INTERNAL
44#include "postgres_fe.h"
@@ -17,9 +17,8 @@ static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
1717static pthread_key_t actual_connection_key ;
1818static pthread_once_t actual_connection_key_once = PTHREAD_ONCE_INIT ;
1919
20- #else
21- static struct connection * actual_connection = NULL ;
2220#endif
21+ static struct connection * actual_connection = NULL ;
2322static struct connection * all_connections = NULL ;
2423
2524#ifdef ENABLE_THREAD_SAFETY
@@ -39,6 +38,16 @@ ecpg_get_connection_nr(const char *connection_name)
3938 {
4039#ifdef ENABLE_THREAD_SAFETY
4140 ret = pthread_getspecific (actual_connection_key );
41+ /* if no connection in TSD for this thread, get the global default connection
42+ * and hope the user knows what they're doing (i.e. using their own mutex to
43+ * protect that connection from concurrent accesses */
44+ if (NULL == ret )
45+ {
46+ ECPGlog ("no TSD connection, going for global\n" );
47+ ret = actual_connection ;
48+ }
49+ else
50+ ECPGlog ("got the TSD connection\n" );
4251#else
4352 ret = actual_connection ;
4453#endif
@@ -67,6 +76,16 @@ ECPGget_connection(const char *connection_name)
6776 {
6877#ifdef ENABLE_THREAD_SAFETY
6978 ret = pthread_getspecific (actual_connection_key );
79+ /* if no connection in TSD for this thread, get the global default connection
80+ * and hope the user knows what they're doing (i.e. using their own mutex to
81+ * protect that connection from concurrent accesses */
82+ if (NULL == ret )
83+ {
84+ ECPGlog ("no TSD connection here either, using global\n" );
85+ ret = actual_connection ;
86+ }
87+ else
88+ ECPGlog ("got TSD connection\n" );
7089#else
7190 ret = actual_connection ;
7291#endif
@@ -117,10 +136,9 @@ ecpg_finish(struct connection * act)
117136#ifdef ENABLE_THREAD_SAFETY
118137 if (pthread_getspecific (actual_connection_key ) == act )
119138 pthread_setspecific (actual_connection_key , all_connections );
120- #else
139+ #endif
121140 if (actual_connection == act )
122141 actual_connection = all_connections ;
123- #endif
124142
125143 ECPGlog ("ecpg_finish: Connection %s closed.\n" , act -> name );
126144
@@ -416,9 +434,8 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
416434#ifdef ENABLE_THREAD_SAFETY
417435 pthread_once (& actual_connection_key_once , ecpg_actual_connection_init );
418436 pthread_setspecific (actual_connection_key , all_connections );
419- #else
420- actual_connection = all_connections ;
421437#endif
438+ actual_connection = all_connections ;
422439
423440 ECPGlog ("ECPGconnect: opening database %s on %s port %s %s%s%s%s\n" ,
424441 realname ? realname : "<DEFAULT>" ,
0 commit comments