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 $ */
2
2
3
3
#define POSTGRES_ECPG_INTERNAL
4
4
#include "postgres_fe.h"
@@ -17,9 +17,8 @@ static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
17
17
static pthread_key_t actual_connection_key ;
18
18
static pthread_once_t actual_connection_key_once = PTHREAD_ONCE_INIT ;
19
19
20
- #else
21
- static struct connection * actual_connection = NULL ;
22
20
#endif
21
+ static struct connection * actual_connection = NULL ;
23
22
static struct connection * all_connections = NULL ;
24
23
25
24
#ifdef ENABLE_THREAD_SAFETY
@@ -39,6 +38,16 @@ ecpg_get_connection_nr(const char *connection_name)
39
38
{
40
39
#ifdef ENABLE_THREAD_SAFETY
41
40
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" );
42
51
#else
43
52
ret = actual_connection ;
44
53
#endif
@@ -67,6 +76,16 @@ ECPGget_connection(const char *connection_name)
67
76
{
68
77
#ifdef ENABLE_THREAD_SAFETY
69
78
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" );
70
89
#else
71
90
ret = actual_connection ;
72
91
#endif
@@ -117,10 +136,9 @@ ecpg_finish(struct connection * act)
117
136
#ifdef ENABLE_THREAD_SAFETY
118
137
if (pthread_getspecific (actual_connection_key )== act )
119
138
pthread_setspecific (actual_connection_key ,all_connections );
120
- #else
139
+ #endif
121
140
if (actual_connection == act )
122
141
actual_connection = all_connections ;
123
- #endif
124
142
125
143
ECPGlog ("ecpg_finish: Connection %s closed.\n" ,act -> name );
126
144
@@ -416,9 +434,8 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
416
434
#ifdef ENABLE_THREAD_SAFETY
417
435
pthread_once (& actual_connection_key_once ,ecpg_actual_connection_init );
418
436
pthread_setspecific (actual_connection_key ,all_connections );
419
- #else
420
- actual_connection = all_connections ;
421
437
#endif
438
+ actual_connection = all_connections ;
422
439
423
440
ECPGlog ("ECPGconnect: opening database %s on %s port %s %s%s%s%s\n" ,
424
441
realname ?realname :"<DEFAULT>" ,