src/server/plugins/postgresql/resource.c

branch
webdav
changeset 271
fd5765c5068c
parent 270
4cfaa02055cd
child 272
f210681d9dd0
equal deleted inserted replaced
270:4cfaa02055cd 271:fd5765c5068c
49 return NULL; 49 return NULL;
50 } 50 }
51 51
52 // test connection 52 // test connection
53 PGconn *test_connection = PQconnectdb(connection); 53 PGconn *test_connection = PQconnectdb(connection);
54 if(pg_check_connection(rpname, test_connection)) { 54 if(pg_check_connection(LOG_WARN, rpname, test_connection)) {
55 return NULL; // error, pg_check_connection does the logging 55 log_ereport(LOG_WARN, "Resource pool %s: Connection check failed", rpname);
56 } 56 }
57 PQfinish(test_connection); 57 if(test_connection) PQfinish(test_connection);
58 58
59 PgResourcePool *pg = pool_malloc(pool, sizeof(PgResourcePool)); 59 PgResourcePool *pg = pool_malloc(pool, sizeof(PgResourcePool));
60 if(!pg) { 60 if(!pg) {
61 return NULL; 61 return NULL;
62 } 62 }
66 66
67 return pg; 67 return pg;
68 68
69 } 69 }
70 70
71 int pg_check_connection(const char *rpname, PGconn *connection) { 71 int pg_check_connection(int loglevel, const char *rpname, PGconn *connection) {
72 if(!connection) { 72 if(!connection) {
73 log_ereport(LOG_FAILURE, "Resource pool %s: Cannot create PQ connection", rpname); 73 log_ereport(loglevel, "Resource pool %s: Cannot create PQ connection", rpname);
74 return 1; 74 return 1;
75 } 75 }
76 if(PQstatus(connection) != CONNECTION_OK) { 76 if(PQstatus(connection) != CONNECTION_OK) {
77 char *err = PQerrorMessage(connection); 77 char *err = PQerrorMessage(connection);
78 int errlen = 0; 78 int errlen = 0;
80 errlen = strlen(err); 80 errlen = strlen(err);
81 if(errlen > 0 && err[errlen-1] == '\n') { 81 if(errlen > 0 && err[errlen-1] == '\n') {
82 errlen--; 82 errlen--;
83 } 83 }
84 } 84 }
85 log_ereport(LOG_FAILURE, "Resource pool %s: Failed to connect to database: %.*s", rpname, errlen, err); 85 log_ereport(loglevel, "Resource pool %s: Failed to connect to database: %.*s", rpname, errlen, err);
86 PQfinish(connection); 86 PQfinish(connection);
87 return 1; 87 return 1;
88 } 88 }
89 return 0; 89 return 0;
90 } 90 }
94 // unused 94 // unused
95 } 95 }
96 96
97 void * pg_resourcepool_createresource(PgResourcePool *pg) { 97 void * pg_resourcepool_createresource(PgResourcePool *pg) {
98 PGconn *connection = PQconnectdb(pg->connection); 98 PGconn *connection = PQconnectdb(pg->connection);
99 if(pg_check_connection(pg->name, connection)) { 99 if(pg_check_connection(LOG_FAILURE, pg->name, connection)) {
100 return NULL; 100 return NULL;
101 } 101 }
102 102
103 PgResource *res = pool_malloc(pg->pool, sizeof(PgResource)); 103 PgResource *res = pool_malloc(pg->pool, sizeof(PgResource));
104 if(!res) { 104 if(!res) {

mercurial