Navigation


Changeset 771:eac79a449c06 in freeDiameter for extensions/app_acct


Ignore:
Timestamp:
Oct 31, 2011, 4:57:59 AM (13 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Making app_acct thread_safe for PGsql. NOT TESTED.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/app_acct/acct_db.c

    r741 r771  
    3636/* Database interface module */
    3737
     38/* There is one connection to the db per thread.
     39The connection is stored in the pthread_key_t variable */
     40
     41
    3842#include "app_acct.h"
    3943#include <libpq-fe.h>
     
    5458static
    5559#endif /* TEST_DEBUG */
    56 PGconn *conn = NULL;
     60pthread_key_t connk;
     61
    5762
    5863/* Initialize the database context: connection to the DB, prepared statement to insert new records */
     
    6671        int idx = 0;
    6772        PGresult * res;
     73        PGconn *conn;
    6874        #define REALLOC_SIZE    1024    /* We extend the buffer by this amount */
    6975       
     
    195201        acct_rec_empty(&emptyrecords);
    196202       
     203        CHECK_POSIX( pthread_key_create(&connk, (void (*)(void*))PQfinish) );
     204        CHECK_POSIX( pthread_setspecific(connk, conn) );
     205       
    197206        /* Ok, ready */
    198207        return 0;
     
    202211void acct_db_free(void)
    203212{       
    204         if (conn) {
    205                 /* Note: the prepared statement is automatically freed when the session terminates */
    206                 PQfinish(conn);
    207                 conn = NULL;
    208         }
     213        CHECK_POSIX_DO(pthread_key_delete(connk) , );
    209214}
    210215
     
    219224        PGresult *res;
    220225        struct fd_list *li;
     226        PGconn *conn;
    221227       
    222228        TRACE_ENTRY("%p", records);
    223         CHECK_PARAMS( conn && records );
     229        CHECK_PARAMS( records );
     230       
     231        conn = pthread_getspecific(connk);
     232        if (!conn) {
     233                conn = PQconnectdb(acct_config->conninfo);
     234                CHECK_POSIX( pthread_setspecific(connk, conn) );
     235        }
    224236       
    225237        /* First, check if the connection with the DB has not staled, and eventually try to fix it */
Note: See TracChangeset for help on using the changeset viewer.