mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Verify database connection on interval (#9665)
This commit is contained in:
parent
62c62f3829
commit
5c588f89e7
9 changed files with 65 additions and 33 deletions
|
@ -90,13 +90,19 @@ void Database_PostgreSQL::connectToDatabase()
|
|||
initStatements();
|
||||
}
|
||||
|
||||
void Database_PostgreSQL::verifyDatabase()
|
||||
void Database_PostgreSQL::pingDatabase()
|
||||
{
|
||||
if (PQstatus(m_conn) == CONNECTION_OK)
|
||||
return;
|
||||
// Verify DB connection with ping
|
||||
try {
|
||||
ping();
|
||||
} catch (const DatabaseException &e) {
|
||||
// If ping failed, show the error and try reconnect
|
||||
PQreset(m_conn);
|
||||
|
||||
PQreset(m_conn);
|
||||
ping();
|
||||
errorstream << e.what() << std::endl
|
||||
<< "Reconnecting to database " << m_connect_string << std::endl;
|
||||
connectToDatabase();
|
||||
}
|
||||
}
|
||||
|
||||
void Database_PostgreSQL::ping()
|
||||
|
@ -151,7 +157,7 @@ void Database_PostgreSQL::createTableIfNotExists(const std::string &table_name,
|
|||
|
||||
void Database_PostgreSQL::beginSave()
|
||||
{
|
||||
verifyDatabase();
|
||||
pingDatabase();
|
||||
checkResults(PQexec(m_conn, "BEGIN;"));
|
||||
}
|
||||
|
||||
|
@ -227,7 +233,7 @@ bool MapDatabasePostgreSQL::saveBlock(const v3s16 &pos, const std::string &data)
|
|||
return false;
|
||||
}
|
||||
|
||||
verifyDatabase();
|
||||
pingDatabase();
|
||||
|
||||
s32 x, y, z;
|
||||
x = htonl(pos.X);
|
||||
|
@ -251,7 +257,7 @@ bool MapDatabasePostgreSQL::saveBlock(const v3s16 &pos, const std::string &data)
|
|||
|
||||
void MapDatabasePostgreSQL::loadBlock(const v3s16 &pos, std::string *block)
|
||||
{
|
||||
verifyDatabase();
|
||||
pingDatabase();
|
||||
|
||||
s32 x, y, z;
|
||||
x = htonl(pos.X);
|
||||
|
@ -275,7 +281,7 @@ void MapDatabasePostgreSQL::loadBlock(const v3s16 &pos, std::string *block)
|
|||
|
||||
bool MapDatabasePostgreSQL::deleteBlock(const v3s16 &pos)
|
||||
{
|
||||
verifyDatabase();
|
||||
pingDatabase();
|
||||
|
||||
s32 x, y, z;
|
||||
x = htonl(pos.X);
|
||||
|
@ -293,7 +299,7 @@ bool MapDatabasePostgreSQL::deleteBlock(const v3s16 &pos)
|
|||
|
||||
void MapDatabasePostgreSQL::listAllLoadableBlocks(std::vector<v3s16> &dst)
|
||||
{
|
||||
verifyDatabase();
|
||||
pingDatabase();
|
||||
|
||||
PGresult *results = execPrepared("list_all_loadable_blocks", 0,
|
||||
NULL, NULL, NULL, false, false);
|
||||
|
@ -435,7 +441,7 @@ void PlayerDatabasePostgreSQL::initStatements()
|
|||
|
||||
bool PlayerDatabasePostgreSQL::playerDataExists(const std::string &playername)
|
||||
{
|
||||
verifyDatabase();
|
||||
pingDatabase();
|
||||
|
||||
const char *values[] = { playername.c_str() };
|
||||
PGresult *results = execPrepared("load_player", 1, values, false);
|
||||
|
@ -451,7 +457,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
|
|||
if (!sao)
|
||||
return;
|
||||
|
||||
verifyDatabase();
|
||||
pingDatabase();
|
||||
|
||||
v3f pos = sao->getBasePosition();
|
||||
std::string pitch = ftos(sao->getLookPitch());
|
||||
|
@ -535,7 +541,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
|
|||
bool PlayerDatabasePostgreSQL::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
|
||||
{
|
||||
sanity_check(sao);
|
||||
verifyDatabase();
|
||||
pingDatabase();
|
||||
|
||||
const char *values[] = { player->getName() };
|
||||
PGresult *results = execPrepared("load_player", 1, values, false, false);
|
||||
|
@ -610,7 +616,7 @@ bool PlayerDatabasePostgreSQL::removePlayer(const std::string &name)
|
|||
if (!playerDataExists(name))
|
||||
return false;
|
||||
|
||||
verifyDatabase();
|
||||
pingDatabase();
|
||||
|
||||
const char *values[] = { name.c_str() };
|
||||
execPrepared("remove_player", 1, values);
|
||||
|
@ -620,7 +626,7 @@ bool PlayerDatabasePostgreSQL::removePlayer(const std::string &name)
|
|||
|
||||
void PlayerDatabasePostgreSQL::listPlayers(std::vector<std::string> &res)
|
||||
{
|
||||
verifyDatabase();
|
||||
pingDatabase();
|
||||
|
||||
PGresult *results = execPrepared("load_player_list", 0, NULL, false);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue