Browse code

- using improved connection pool

Jan Janak authored on 02/02/2005 19:09:22
Showing 3 changed files
... ...
@@ -35,10 +35,10 @@
35 35
 #include <mysql/errmsg.h>
36 36
 #include "../../mem/mem.h"
37 37
 #include "../../dprint.h"
38
+#include "../../db/db_pool.h"
38 39
 #include "utils.h"
39 40
 #include "val.h"
40 41
 #include "my_con.h"
41
-#include "my_pool.h"
42 42
 #include "res.h"
43 43
 #include "db_mod.h"
44 44
 #include "dbase.h"
... ...
@@ -85,7 +85,7 @@ static int submit_query(db_con_t* _h, const char* _s)
85 85
 	 * value shouldn't be needed, but it doesn't hurt either, since the loop
86 86
 	 * will most of the time stop at the second or sometimes at the third
87 87
 	 * iteration.
88
-     */
88
+	 */
89 89
 	for (i=0; i<(auto_reconnect ? 3 : 1); i++) {
90 90
 		if (mysql_query(CON_CONNECTION(_h), _s)==0) {
91 91
 			return 0;
... ...
@@ -243,8 +243,13 @@ static int print_set(MYSQL* _c, char* _b, int _l, db_key_t* _k, db_val_t* _v, in
243 243
  */
244 244
 db_con_t* db_init(const char* _url)
245 245
 {
246
+	struct db_id* id;
247
+	struct my_con* con;
246 248
 	db_con_t* res;
247 249
 
250
+	id = 0;
251
+	res = 0;
252
+
248 253
 	if (!_url) {
249 254
 		LOG(L_ERR, "db_init: Invalid parameter value\n");
250 255
 		return 0;
... ...
@@ -257,15 +262,34 @@ db_con_t* db_init(const char* _url)
257 262
 	}
258 263
 	memset(res, 0, sizeof(db_con_t) + sizeof(struct my_con*));
259 264
 
260
-	res->tail = (unsigned long)get_connection(_url);
261
-
262
-	if (!res->tail) {
263
-		LOG(L_ERR, "db_init: Could not create a connection\n");
264
-		pkg_free(res);
265
-		return 0;
265
+	id = new_db_id(_url);
266
+	if (!id) {
267
+		LOG(L_ERR, "db_init: Cannot parse URL '%s'\n", _url);
268
+		goto err;
269
+	}
270
+
271
+	     /* Find the connection in the pool */
272
+	con = (struct my_con*)pool_get(id);
273
+	if (!con) {
274
+		DBG("db_init: Connection '%s' not found in pool\n", _url);
275
+		     /* Not in the pool yet */
276
+		con = new_connection(id);
277
+		if (!con) {
278
+			LOG(L_ERR, "db_init: No memory left\n");
279
+			goto err;
280
+		}
281
+		pool_insert((struct pool_con*)con);
282
+	} else {
283
+		DBG("db_init: Connection '%s' found in pool\n", _url);
266 284
 	}
267 285
 
286
+	res->tail = (unsigned long)con;
268 287
 	return res;
288
+
289
+ err:
290
+	if (id) free_db_id(id);
291
+	if (res) pkg_free(res);
292
+	return 0;
269 293
 }
270 294
 
271 295
 
... ...
@@ -275,12 +299,18 @@ db_con_t* db_init(const char* _url)
275 299
  */
276 300
 void db_close(db_con_t* _h)
277 301
 {
302
+	struct pool_con* con;
303
+
278 304
 	if (!_h) {
279 305
 		LOG(L_ERR, "db_close: Invalid parameter value\n");
280 306
 		return;
281 307
 	}
282 308
 
283
-	release_connection((struct my_con*)_h->tail);
309
+	con = (struct pool_con*)_h->tail;
310
+	if (pool_remove(con) != 0) {
311
+		free_connection((struct my_con*)con);
312
+	}
313
+
284 314
 	pkg_free(_h);
285 315
 }
286 316
 
... ...
@@ -1,7 +1,6 @@
1 1
 /* 
2 2
  * $Id$
3 3
  *
4
- *
5 4
  * Copyright (C) 2001-2004 iptel.org
6 5
  *
7 6
  * This file is part of ser, a free SIP server.
... ...
@@ -26,20 +25,20 @@
26 25
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 26
  */
28 27
 
29
-#include <string.h>
30
-#include <time.h>
31 28
 #include "my_con.h"
32 29
 #include "../../mem/mem.h"
33 30
 #include "../../dprint.h"
34 31
 #include "../../ut.h"
35 32
 #include "utils.h"
33
+#include <string.h>
34
+#include <time.h>
36 35
 
37 36
 
38 37
 /*
39 38
  * Create a new connection structure,
40 39
  * open the MySQL connection and set reference count to 1
41 40
  */
42
-struct my_con* new_connection(struct my_id* id)
41
+struct my_con* new_connection(struct db_id* id)
43 42
 {
44 43
 	struct my_con* ptr;
45 44
 
... ...
@@ -66,23 +65,23 @@ struct my_con* new_connection(struct my_id* id)
66 65
 	mysql_init(ptr->con);
67 66
 
68 67
 	if (id->port) {
69
-		DBG("new_connection: Opening MySQL connection: mysql://%.*s:%.*s@%.*s:%d/%.*s\n",
70
-		    id->username.len, ZSW(id->username.s),
71
-		    id->password.len, ZSW(id->password.s),
72
-		    id->host.len, ZSW(id->host.s),
68
+		DBG("new_connection: Opening MySQL connection: mysql://%s:%s@%s:%d/%s\n",
69
+		    ZSW(id->username),
70
+		    ZSW(id->password),
71
+		    ZSW(id->host),
73 72
 		    id->port,
74
-		    id->database.len, ZSW(id->database.s)
73
+		    ZSW(id->database)
75 74
 		    );
76 75
 	} else {
77
-		DBG("new_connection: Opening MySQL connection: mysql://%.*s:%.*s@%.*s/%.*s\n",
78
-		    id->username.len, ZSW(id->username.s),
79
-		    id->password.len, ZSW(id->password.s),
80
-		    id->host.len, ZSW(id->host.s),
81
-		    id->database.len, ZSW(id->database.s)
76
+		DBG("new_connection: Opening MySQL connection: mysql://%s:%s@%s/%s\n",
77
+		    ZSW(id->username),
78
+		    ZSW(id->password),
79
+		    ZSW(id->host),
80
+		    ZSW(id->database)
82 81
 		    );
83 82
 	}
84 83
 
85
-	if (!mysql_real_connect(ptr->con, id->host.s, id->username.s, id->password.s, id->database.s, id->port, 0, 0)) {
84
+	if (!mysql_real_connect(ptr->con, id->host, id->username, id->password, id->database, id->port, 0, 0)) {
86 85
 		LOG(L_ERR, "new_connection: %s\n", mysql_error(ptr->con));
87 86
 		mysql_close(ptr->con);
88 87
 		goto err;
... ...
@@ -94,9 +93,7 @@ struct my_con* new_connection(struct my_id* id)
94 93
 
95 94
 
96 95
 	ptr->timestamp = time(0);
97
-
98 96
 	ptr->id = id;
99
-	
100 97
 	return ptr;
101 98
 
102 99
  err:
... ...
@@ -113,7 +110,7 @@ void free_connection(struct my_con* con)
113 110
 {
114 111
 	if (!con) return;
115 112
 	if (con->res) mysql_free_result(con->res);
116
-	if (con->id) free_my_id(con->id);
113
+	if (con->id) free_db_id(con->id);
117 114
 	if (con->con) {
118 115
 		mysql_close(con->con);
119 116
 		pkg_free(con->con);
... ...
@@ -1,7 +1,6 @@
1 1
 /* 
2 2
  * $Id$
3 3
  *
4
- *
5 4
  * Copyright (C) 2001-2003 FhG Fokus
6 5
  *
7 6
  * This file is part of ser, a free SIP server.
... ...
@@ -29,18 +28,22 @@
29 28
 #ifndef MY_CON_H
30 29
 #define MY_CON_H
31 30
 
31
+#include "../../db/db_pool.h"
32
+#include "../../db/db_id.h"
33
+
32 34
 #include <time.h>
33 35
 #include <mysql/mysql.h>
34
-#include "my_id.h"
36
+
35 37
 
36 38
 struct my_con {
37
-	struct my_id* id;    /* Connection identifier */
38
-	int ref;             /* Reference count */
39
-	MYSQL_RES* res;      /* Actual result */
40
-	MYSQL* con;          /* Connection representation */
41
-	MYSQL_ROW row;       /* Actual row in the result */
42
-	time_t timestamp;    /* Timestamp of last query */
43
-	struct my_con* next; /* Next connection in the pool */
39
+	struct db_id* id;        /* Connection identifier */
40
+	unsigned int ref;        /* Reference count */
41
+	struct pool_con* next;   /* Next connection in the pool */
42
+
43
+	MYSQL_RES* res;          /* Actual result */
44
+	MYSQL* con;              /* Connection representation */
45
+	MYSQL_ROW row;           /* Actual row in the result */
46
+	time_t timestamp;        /* Timestamp of last query */
44 47
 };
45 48
 
46 49
 
... ...
@@ -57,7 +60,7 @@ struct my_con {
57 60
  * Create a new connection structure,
58 61
  * open the MySQL connection and set reference count to 1
59 62
  */
60
-struct my_con* new_connection(struct my_id* id);
63
+struct my_con* new_connection(struct db_id* id);
61 64
 
62 65
 
63 66
 /*