Raised This Month: $51 Target: $400
 12% 

How to build reliable MySQL connection on server start?


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 12-16-2019 , 03:00   How to build reliable MySQL connection on server start?
Reply With Quote #1

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:
Spoiler


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_ConnectHX_DATABASE_CFG);
        }
    }
}

Action Timer_SQL_ReConnect(Handle timer)
{
    
OnConfigsExecuted();
}

public 
void SQL_Callback_Connect (Database db, const char[] errorany data)
{
    const 
int MAX_ATTEMPT 20;
    static 
int iAttempt;
    
g_hDB db;
    if (!
db)
    {
        ++
iAttempt;
        
LogError("Attempt #%i. %s"iAttempterror);
        
        if (
iAttempt MAX_ATTEMPT)
        {
            
CreateTimer(3.0Timer_SQL_ReConnect_TIMER_FLAG_NO_MAPCHANGE);
        }
        else {
            
iAttempt 0;
        }
        return;
    }
    
SQL_OnConnected();
}

void SQL_OnConnected()
{
}

public 
void Event_SQL_Save(Event event, const char[] namebool 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?
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 19:13.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode