Hi,
rarely I'm receiving the following error: "Can't connect to MySQL server" right after server restart (sometimes).
My SQL DB is external. I don't think there is any problem with hoster.
sm: 1.10.6454
Log:
PHP Code:
public void OnPluginStart()
{
HookEvent("round_end", Event_SQL_Save, EventHookMode_PostNoCopy);
}
public void OnConfigsExecuted()
{
if (!g_hDB)
{
if (SQL_CheckConfig(HX_DATABASE_CFG))
{
Database.Connect(SQL_Callback_Connect, HX_DATABASE_CFG);
}
}
}
Action Timer_SQL_ReConnect(Handle timer)
{
OnConfigsExecuted();
}
public void SQL_Callback_Connect (Database db, const char[] error, any data)
{
const int MAX_ATTEMPT = 20;
static int iAttempt;
g_hDB = db;
if (!db)
{
++iAttempt;
LogError("Attempt #%i. %s", iAttempt, error);
if (iAttempt < MAX_ATTEMPT)
{
CreateTimer(3.0, Timer_SQL_ReConnect, _, TIMER_FLAG_NO_MAPCHANGE);
}
else {
iAttempt = 0;
}
return;
}
SQL_OnConnected();
}
void SQL_OnConnected()
{
}
public void Event_SQL_Save(Event event, const char[] name, bool dontBroadcast)
{
// ...
delete g_hDB;
}
1) Is OnConfigsExecuted() correct place for starting connection?
2) Do I need to close handle on plugin end?
3) What is a best practice to keep db connection alive? How sm handle this, e.g. when connection established and it was lost for some time (several sec. or so)?
4) Did I accomlished it correctly (in reliability meaning) so I close(?) connection on round_end (and Map End) and restore it again each time?
__________________