11#include " ReadoutDatabase.h"
22#include < mysql.h>
3+ #include < mysql/errmsg.h>
34#include < string.h>
45#include < stdlib.h>
56#include < stdarg.h>
89#include < inttypes.h>
910#include < string>
1011#include < map>
12+ #include < time.h>
1113
1214#if LIBMYSQL_VERSION_ID >= 80000
1315typedef bool my_bool;
1416#endif
1517
18+ int ReadoutDatabase::connect () {
19+ time_t now = time (NULL );
20+ if ((lastConnectTime != 0 ) && (now < lastConnectTime + reconnectTimeout)) return -2 ;
21+ lastConnectTime = now;
22+ if (db != nullptr ) {
23+ mysql_close (db);
24+ db = nullptr ;
25+ }
26+ db = mysql_init (nullptr );
27+ if (db == nullptr ) {
28+ return -1 ;
29+ }
30+ if (mysql_real_connect (db,cxDbHost.c_str (),cxDbUser.c_str (),cxDbPwd.c_str (),cxDbName.c_str (),0 ,nullptr ,0 ) == nullptr ) {
31+ log (std::string (" DB connect error :" ) + mysql_error (db));
32+ return -1 ;
33+ }
34+ log (" DB connected" );
35+ lastConnectTime = 0 ;
36+ return 0 ;
37+ }
38+
1639ReadoutDatabase::ReadoutDatabase (const char * cx, int v, const LogCallback& cb) {
1740 verbose = v;
1841 theLogCallback = cb;
1942
2043 char *db_db=nullptr , *db_user=nullptr , *db_pwd=nullptr , *db_host=nullptr ;
2144 char *p=nullptr ,*ptr,*lptr;
22- my_bool reconnect = 1 ;
2345
2446 if (cx == nullptr ) {
2547 throw __LINE__;
@@ -50,6 +72,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) {
5072 break ;
5173 }
5274 }
75+ cxDbUser = db_user;
5376
5477 // pwd
5578 for (lptr=ptr;*ptr!=0 ;ptr++) {
@@ -60,6 +83,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) {
6083 break ;
6184 }
6285 }
86+ cxDbPwd = db_pwd;
6387
6488 // host
6589 for (lptr=ptr;*ptr!=0 ;ptr++) {
@@ -70,6 +94,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) {
7094 break ;
7195 }
7296 }
97+ cxDbHost = db_host;
7398
7499 // db name
75100 db_db=ptr;
@@ -82,12 +107,7 @@ ReadoutDatabase::ReadoutDatabase(const char* cx, int v, const LogCallback& cb) {
82107 log (" Using database " + std::string (db_db) + " @" + std::string (db_host));
83108
84109 // try to connect
85- if (mysql_real_connect (db,db_host,db_user,db_pwd,db_db,0 ,nullptr ,0 )==nullptr ) {
86- goto open_failed;
87- }
88-
89-
90- if (mysql_options (db, MYSQL_OPT_RECONNECT, &reconnect)) {
110+ if (this ->connect ()) {
91111 goto open_failed;
92112 }
93113
@@ -167,14 +187,27 @@ int ReadoutDatabase::query(int maxRetry, const char *inQuery,...) {
167187
168188 int i;
169189 for (i=1 ; i<=maxRetry; i++) {
170- if (mysql_query (db,query)== 0 ) break ;
190+ if (mysql_query (db,query) == 0 ) break ;
171191 lastError = std::string (" DB query error :" ) + mysql_error (db);
192+ log (" DB error: " + std::to_string (mysql_errno (db)) + " = " + lastError);
193+ if (mysql_errno (db) == CR_SERVER_LOST) {
194+ log (" DB trying to reconnect" );
195+ int err = connect ();
196+ if (err == 0 ) {
197+ continue ;
198+ } else {
199+ if (err == -2 ) {
200+ log (" DB reconnect - need to wait a bit before retry" );
201+ }
202+ return -1 ;
203+ }
204+ }
172205 usleep (retryTimeout);
173206 }
174207 if (i > maxRetry) {
175208 return -1 ;
176209 }
177-
210+ log ( " DB query success " );
178211 return 0 ;
179212}
180213
0 commit comments