km_my_con.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2004 iptel.org
  5. * Copyright (C) 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. /*! \file
  24. * \brief DB_MYSQL :: Connections
  25. * \ingroup db_mysql
  26. * Module: \ref db_mysql
  27. */
  28. #include "km_my_con.h"
  29. #include "km_db_mysql.h"
  30. #include <mysql/mysql_version.h>
  31. #include "../../mem/mem.h"
  32. #include "../../dprint.h"
  33. #include "../../ut.h"
  34. #include "mysql_mod.h"
  35. /*! \brief
  36. * Create a new connection structure,
  37. * open the MySQL connection and set reference count to 1
  38. */
  39. struct my_con* db_mysql_new_connection(const struct db_id* id)
  40. {
  41. struct my_con* ptr;
  42. char *host, *grp;
  43. unsigned int connection_flag = 0;
  44. if (!id) {
  45. LM_ERR("invalid parameter value\n");
  46. return 0;
  47. }
  48. ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con));
  49. if (!ptr) {
  50. LM_ERR("no private memory left\n");
  51. return 0;
  52. }
  53. memset(ptr, 0, sizeof(struct my_con));
  54. ptr->ref = 1;
  55. ptr->con = (MYSQL*)pkg_malloc(sizeof(MYSQL));
  56. if (!ptr->con) {
  57. LM_ERR("no private memory left\n");
  58. goto err;
  59. }
  60. mysql_init(ptr->con);
  61. if (id->host[0] == '[' && (host = strchr(id->host, ']')) != NULL) {
  62. grp = id->host + 1;
  63. *host = '\0';
  64. if (host != id->host + strlen(id->host)-1) {
  65. host += 1; // host found after closing bracket
  66. }
  67. else {
  68. // let mysql read host info from my.cnf
  69. // (defaults to "localhost")
  70. host = NULL;
  71. }
  72. // read [client] and [<grp>] sections in the order
  73. // given in my.cnf
  74. mysql_options(ptr->con, MYSQL_READ_DEFAULT_GROUP, grp);
  75. }
  76. else {
  77. host = id->host;
  78. }
  79. if (id->port) {
  80. LM_DBG("opening connection: mysql://xxxx:xxxx@%s:%d/%s\n", ZSW(host),
  81. id->port, ZSW(id->database));
  82. } else {
  83. LM_DBG("opening connection: mysql://xxxx:xxxx@%s/%s\n", ZSW(host),
  84. ZSW(id->database));
  85. }
  86. // set connect, read and write timeout, the value counts three times
  87. mysql_options(ptr->con, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&db_mysql_timeout_interval);
  88. mysql_options(ptr->con, MYSQL_OPT_READ_TIMEOUT, (const char *)&db_mysql_timeout_interval);
  89. mysql_options(ptr->con, MYSQL_OPT_WRITE_TIMEOUT, (const char *)&db_mysql_timeout_interval);
  90. if (db_mysql_update_affected_found) {
  91. connection_flag |= CLIENT_FOUND_ROWS;
  92. }
  93. #if (MYSQL_VERSION_ID >= 40100)
  94. if (!mysql_real_connect(ptr->con, host, id->username, id->password,
  95. id->database, id->port, 0, connection_flag|CLIENT_MULTI_STATEMENTS)) {
  96. #else
  97. if (!mysql_real_connect(ptr->con, host, id->username, id->password,
  98. id->database, id->port, 0, connection_flag)) {
  99. #endif
  100. LM_ERR("driver error: %s\n", mysql_error(ptr->con));
  101. /* increase error counter */
  102. counter_inc(mysql_cnts_h.driver_err);
  103. mysql_close(ptr->con);
  104. goto err;
  105. }
  106. /* force reconnection if enabled */
  107. if (db_mysql_auto_reconnect)
  108. ptr->con->reconnect = 1;
  109. else
  110. ptr->con->reconnect = 0;
  111. LM_DBG("connection type is %s\n", mysql_get_host_info(ptr->con));
  112. LM_DBG("protocol version is %d\n", mysql_get_proto_info(ptr->con));
  113. LM_DBG("server version is %s\n", mysql_get_server_info(ptr->con));
  114. ptr->timestamp = time(0);
  115. ptr->id = (struct db_id*)id;
  116. return ptr;
  117. err:
  118. if (ptr && ptr->con) pkg_free(ptr->con);
  119. if (ptr) pkg_free(ptr);
  120. return 0;
  121. }
  122. /*! \brief
  123. * Close the connection and release memory
  124. */
  125. void db_mysql_free_connection(struct pool_con* con)
  126. {
  127. struct my_con * _c;
  128. if (!con) return;
  129. _c = (struct my_con*) con;
  130. if (_c->id) free_db_id(_c->id);
  131. if (_c->con) {
  132. mysql_close(_c->con);
  133. pkg_free(_c->con);
  134. }
  135. pkg_free(_c);
  136. }