db_pool.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: db_pool.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * Copyright (C) 2001-2005 iptel.org
00005  * Copyright (C) 2007-2008 1&1 Internet AG
00006  *
00007  * This file is part of Kamailio, a free SIP server.
00008  *
00009  * Kamailio is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * Kamailio is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License 
00020  * along with this program; if not, write to the Free Software 
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00024 /**
00025  * \file db/db_pool.c
00026  * \brief Functions for managing a pool of database connections.
00027  * \ingroup db
00028  */
00029 
00030 #include "../dprint.h"
00031 #include "db_pool.h"
00032 
00033 
00034 /* The head of the pool */
00035 static struct pool_con* db_pool = 0;
00036 
00037 
00038 /*
00039  * Search the pool for a connection with
00040  * the identifier equal to id, NULL is returned
00041  * when no connection is found
00042  */
00043 struct pool_con* pool_get(const struct db_id* id)
00044 {
00045    struct pool_con* ptr;
00046 
00047    if (!id) {
00048       LM_ERR("invalid parameter value\n");
00049       return 0;
00050    }
00051 
00052    ptr = db_pool;
00053    while (ptr) {
00054       if (cmp_db_id(id, ptr->id)) {
00055          ptr->ref++;
00056          return ptr;
00057       }
00058       ptr = ptr->next;
00059    }
00060 
00061    return 0;
00062 }
00063 
00064 
00065 /*
00066  * Insert a new connection into the pool
00067  */
00068 void pool_insert(struct pool_con* con)
00069 {
00070    if (!con) return;
00071 
00072    con->next = db_pool;
00073    db_pool = con;
00074 }
00075 
00076 
00077 /*
00078  * Release connection from the pool, the function
00079  * would return 1 when if the connection is not
00080  * referenced anymore and thus can be closed and
00081  * deleted by the backend. The function returns
00082  * 0 if the connection should still be kept open
00083  * because some other module is still using it.
00084  * The function returns -1 if the connection is
00085  * not in the pool.
00086  */
00087 int pool_remove(struct pool_con* con)
00088 {
00089    struct pool_con* ptr;
00090 
00091    if (!con) return -2;
00092 
00093    if (con->ref > 1) {
00094            /* There are still other users, just
00095             * decrease the reference count and return
00096             */
00097       LM_DBG("connection still kept in the pool\n");
00098       con->ref--;
00099       return 0;
00100    }
00101 
00102    LM_DBG("removing connection from the pool\n");
00103 
00104    if (db_pool == con) {
00105       db_pool = db_pool->next;
00106    } else {
00107       ptr = db_pool;
00108       while(ptr) {
00109          if (ptr->next == con) break;
00110          ptr = ptr->next;
00111       }
00112       if (!ptr) {
00113          LM_ERR("weird, connection not found in the pool\n");
00114          return -1;
00115       } else {
00116               /* Remove the connection from the pool */
00117          ptr->next = con->next;
00118       }
00119    }
00120 
00121    return 1;
00122 }

Generated on Tue May 22 14:00:25 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6