Code

utils dbi: Added sdb_dbi_client_check_conn().
authorSebastian Harl <sh@tokkee.org>
Wed, 20 Feb 2013 21:50:15 +0000 (22:50 +0100)
committerSebastian Harl <sh@tokkee.org>
Wed, 20 Feb 2013 21:50:15 +0000 (22:50 +0100)
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
src/utils/dbi.c

index 77d004296d333acaefc7460677a246765e182423..c2490410de8b5a38cd66b8c89db85f0e1b9bd1df 100644 (file)
@@ -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
index ea846b0006c545e1a3e25b4196cd1bc23f5b5904..380a841321a1c6507f0c6f07a7f322e2d3e8ba6b 100644 (file)
@@ -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, ...)