db_pool.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2005 iptel.org
  5. * Copyright (C) 2007-2008 1&1 Internet AG
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * \file lib/srdb1/db_pool.c
  25. * \brief Functions for managing a pool of database connections.
  26. * \ingroup db1
  27. */
  28. #include "../../dprint.h"
  29. #include "db_pool.h"
  30. /* The head of the pool */
  31. static struct pool_con* db_pool = 0;
  32. /*
  33. * Search the pool for a connection with
  34. * the identifier equal to id, NULL is returned
  35. * when no connection is found
  36. */
  37. struct pool_con* pool_get(const struct db_id* id)
  38. {
  39. struct pool_con* ptr;
  40. if (!id) {
  41. LM_ERR("invalid parameter value\n");
  42. return 0;
  43. }
  44. ptr = db_pool;
  45. while (ptr) {
  46. if (cmp_db_id(id, ptr->id)) {
  47. ptr->ref++;
  48. return ptr;
  49. }
  50. ptr = ptr->next;
  51. }
  52. return 0;
  53. }
  54. /*
  55. * Insert a new connection into the pool
  56. */
  57. void pool_insert(struct pool_con* con)
  58. {
  59. if (!con) return;
  60. con->next = db_pool;
  61. db_pool = con;
  62. }
  63. /*
  64. * Release connection from the pool, the function
  65. * would return 1 when if the connection is not
  66. * referenced anymore and thus can be closed and
  67. * deleted by the backend. The function returns
  68. * 0 if the connection should still be kept open
  69. * because some other module is still using it.
  70. * The function returns -1 if the connection is
  71. * not in the pool.
  72. */
  73. int pool_remove(struct pool_con* con)
  74. {
  75. struct pool_con* ptr;
  76. if (!con) return -2;
  77. if (con->ref > 1) {
  78. /* There are still other users, just
  79. * decrease the reference count and return
  80. */
  81. LM_DBG("connection still kept in the pool\n");
  82. con->ref--;
  83. return 0;
  84. }
  85. LM_DBG("removing connection from the pool\n");
  86. if (db_pool == con) {
  87. db_pool = db_pool->next;
  88. } else {
  89. ptr = db_pool;
  90. while(ptr) {
  91. if (ptr->next == con) break;
  92. ptr = ptr->next;
  93. }
  94. if (!ptr) {
  95. LM_ERR("weird, connection not found in the pool\n");
  96. return -1;
  97. } else {
  98. /* Remove the connection from the pool */
  99. ptr->next = con->next;
  100. }
  101. }
  102. return 1;
  103. }