View Single Post
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 01-10-2020 , 11:39   Re: How to build reliable MySQL connection on server start?
Reply With Quote #7

Quote:
Originally Posted by Dragokas View Post
Why?
If you connect to the database successfully and then you lose connection, you don't have to connect again (if you save the database globally - example - in hDatabase variable. The hDatabase will be valid when the dabatase is on again.

Just connect to the database OnMapStart (or OnConfigsExecuted) and check if the db is null.

PHP Code:

Database hDatabase
;

public 
void OnPluginStart()
{
    
HookEvent("round_end"Event_SQL_SaveEventHookMode_PostNoCopy);
}

public 
void OnConfigsExecuted()
{
    if (!
hDatabase)
    {
        if (
SQL_CheckConfig(HX_DATABASE_CFG))
        {
            
Database.Connect(SQL_Callback_ConnectHX_DATABASE_CFG);
        }
    }


public 
void SQL_Callback_Connect (Database db, const char[] errorany data)
{
    if (!
db)
    {
        
LogError("Could not connect to the database: %s"error);
        return;
    }

    
hDatabase db;
}

public 
void Event_SQL_Save(Event event, const char[] namebool dontBroadcast)
{
    if (!
hDatabase)
    {
        return; 
// no db connection
    
}

    
// ...
    
    // delete g_hDB; // why are you deleting the db here ???

This should work well, except deleting the database when the round ends.
__________________

Last edited by Ilusion9; 01-10-2020 at 11:52.
Ilusion9 is offline