I found the cause of my issue.

In the source lib/srdb1/* some kind of pooling is used.
When kamailio needs a database connection, the cmp_db_id function checks to see if there is already a database connection with the exact same params (username, database, server etc..).

According to the PQ library every proces / thread should have it's own database connection.
The db_postgres is setup to use asynchronous I/O with the PQ library.
So when two threads are using the same database connection it is possible that,
the resultset from a query of thread A is returned to thread B.
The cmp_db_id function in lib/srdb1/db_id.c does _not_ check the PID.

I've created a small patch to fix the problems mentioned above.

The changes are made in lib/srdb1/* files
So if you apply them, it will be active for _every_ database module.
Maybe it's not needed for the mysql database,
but I *assume* it will not break when used on a mysql database. (not tested)
Maybe you will get a bit more database connections, but that depends on which modules you're using.

What did I do:

   * Added pid to the db_id struct.
   * During initialization the pid is saved into the db_id struct.
   * In cmp_db_id the pid is also checked when searching for a database
     connection to be reused.

With kind Regards,
Robert Verspuy

--
*Exa-Omicron*
Patroonsweg 10
3892 DB Zeewolde
Tel.: 088-OMICRON (66 427 66)
http://www.exa-omicron.nl
diff --git a/lib/srdb1/db_id.c b/lib/srdb1/db_id.c
index d55c586..de93862 100644
--- a/lib/srdb1/db_id.c
+++ b/lib/srdb1/db_id.c
@@ -241,6 +241,7 @@ struct db_id* new_db_id(const str* url)
                LM_ERR("error while parsing database URL: '%.*s' \n", url->len, 
url->s);
                goto err;
        }
+       ptr->pid=getpid();
 
        return ptr;
 
@@ -260,6 +261,7 @@ unsigned char cmp_db_id(const struct db_id* id1, const 
struct db_id* id2)
 {
        if (!id1 || !id2) return 0;
        if (id1->port != id2->port) return 0;
+       if (((int)id1->pid) != ((int)id2->pid)) return 0;
 
        if (strcmp(id1->scheme, id2->scheme)) return 0;
        if (id1->username!=0 && id2->username!=0) {
diff --git a/lib/srdb1/db_id.h b/lib/srdb1/db_id.h
index 025855f..1e282c6 100644
--- a/lib/srdb1/db_id.h
+++ b/lib/srdb1/db_id.h
@@ -31,6 +31,7 @@
 #define _DB1_ID_H
 
 #include "../../str.h"
+#include <sys/types.h>
 
 /** Structure representing a database ID */
 struct db_id {
@@ -40,6 +41,7 @@ struct db_id {
        char* host;          /**< Host or IP, case insensitive */
        unsigned short port; /**< Port number */
        char* database;      /**< Database, case sensitive */
+       pid_t pid;           /**< Process ID (only allow one database 
connection per thread */
 };
 
 
_______________________________________________
sr-dev mailing list
sr-dev@lists.sip-router.org
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev

Reply via email to