Skip to content

Commit

Permalink
Added MySQL DB cleanup on shutdown. Also added a potential error-reco…
Browse files Browse the repository at this point in the history
…very handler for error 2006 (DB has gone away).
  • Loading branch information
luciensadi committed May 29, 2017
1 parent 58a754f commit b3a4885
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ char *colorize(struct descriptor_data *d, char *str);

/* extern fcnts */
extern void DBInit();
extern void DBFinalize();
void boot_world(void);
void zone_update(void);
void spec_update(void);
Expand Down Expand Up @@ -359,6 +360,8 @@ void init_game(int port)

close(mother_desc);

DBFinalize();

if (circle_reboot) {
log("Rebooting.");
exit(52); /* what's so great about HHGTTG, anyhow? */
Expand Down
6 changes: 6 additions & 0 deletions src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ void DBInit()
log("Boot db -- DONE.");
}

/* A simple method to clean up after our DB. */
void DBFinalize() {
if (mysql)
mysql_close(mysql);
}

/* reset the time in the game from file
weekday is lost on reboot.... implement
something different if this is mission
Expand Down
30 changes: 27 additions & 3 deletions src/newdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,35 @@ int mysql_wrapper(MYSQL *mysql, char *query)
#endif

int result = mysql_query(mysql, query);

if (mysql_errno(mysql)) {
int errnum = mysql_errno(mysql);

if (errnum) {
sprintf(buf, "MYSQLERROR: %s", mysql_error(mysql));
log(buf);
// log(query);
sprintf(buf, "Offending query: %s", query);
log(buf);

// Recovery procedures for certain errors.
switch (errnum) {
case 2006:
// 'MySQL server has gone away.'
log("The MySQL server connection appears to have dropped. Attempting to establish a new one.");
mysql_close(mysql);
mysql = mysql_init(NULL);
if (!mysql_real_connect(mysql, mysql_host, mysql_user, mysql_password, mysql_db, 0, NULL, 0)) {
sprintf(buf, "FATAL ERROR: %s\r\n", mysql_error(mysql));
log(buf);
log("Suggestion: Make sure your DB is running and that you've specified your connection info in src/mysql_config.cpp.\r\n");

// High chance this won't succeed-- the calling function will likely attempt to read
// the results of the query, but the query had no results and will refuse a read.
// This is crash-inducing behavior 99% of the time.
shutdown();
}
break;
default:
break;
}
}
return result;
}
Expand Down

0 comments on commit b3a4885

Please sign in to comment.