From b5e349aa592e8cf4b946e82283a98a14968de192 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Wed, 20 Feb 2013 22:50:15 +0100 Subject: [PATCH] utils dbi: Added sdb_dbi_client_check_conn(). This functions checks the database connection and tries to reconnect if there was a problem. The return code determines if the connection was usable afterwards. --- src/include/utils/dbi.h | 11 +++++++++++ src/utils/dbi.c | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/include/utils/dbi.h b/src/include/utils/dbi.h index 77d0042..c249041 100644 --- a/src/include/utils/dbi.h +++ b/src/include/utils/dbi.h @@ -113,6 +113,17 @@ sdb_dbi_client_set_options(sdb_dbi_client_t *client, int sdb_dbi_client_connect(sdb_dbi_client_t *client); +/* + * sdb_dbi_client_check_conn: + * Check the database-connection and try to reconnect if that fails. + * + * Returns: + * - 0 on success (the connection is connected after the call) + * - a negative value else (failed to reconnect) + */ +int +sdb_dbi_client_check_conn(sdb_dbi_client_t *client); + /* * sdb_dbi_exec_query: * Execute an SQL query on the database. The specified 'callback' will be diff --git a/src/utils/dbi.c b/src/utils/dbi.c index ea846b0..380a841 100644 --- a/src/utils/dbi.c +++ b/src/utils/dbi.c @@ -346,6 +346,20 @@ sdb_dbi_client_connect(sdb_dbi_client_t *client) return 0; } /* sdb_dbi_client_connect */ +int +sdb_dbi_client_check_conn(sdb_dbi_client_t *client) +{ + if (! client) + return -1; + + if (! client->conn) + return sdb_dbi_client_connect(client); + + if (dbi_conn_ping(client->conn)) + return 0; + return sdb_dbi_client_connect(client); +} /* sdb_dbi_client_check_conn */ + int sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query, sdb_dbi_data_cb callback, sdb_object_t *user_data, int n, ...) -- 2.30.2