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

Database reconnect on lost connection?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
foon
Member
Join Date: Dec 2018
Old 04-29-2020 , 22:25   Database reconnect on lost connection?
Reply With Quote #1

Im wondering, when you make a connection to a database and you lose connection to it, will it auto-reconnect on the next map start, or do I have to connect again?

Example, do I have to do this or can I get rid of the "OnMapStart()"?
Code:
Database g_hDatabase = null;

public void OnPluginStart()
{
	Database.Connect(SQL_Connection, "db");
}

public void OnMapStart()
{
	if (g_hDatabase == null)
	{
		Database.Connect(SQL_Connection, "db");
	}
}


public void SQL_Connection(Database hDatabase, const char[] sError, int iData)
{
	if (hDatabase == null)
	{
		ThrowError(sError);
	}
	else
	{
		g_hDatabase = hDatabase;
		
		PrintToServer("[SQL] Connected to DB.");
		return;
	}
}
foon is offline
Balimbanana
Member
Join Date: Jan 2017
Old 04-29-2020 , 23:29   Re: Database reconnect on lost connection?
Reply With Quote #2

I usually check it on the query to INSERT or UPDATE callback for the sError or INVALID_HANDLE.
Code:
public void SQL_QueryCB(Database hDatabase, const char[] sError, int iData)
{
	if ((StrContains(sError, "lost connection", false) != -1) || (StrContains(sError, "unknown mysql server host", false) != -1) || (StrContains(sError, "Can't connect to", false) != -1) || (hndl == INVALID_HANDLE))
	{
		PrintToServer("[SQL] retry connection: %s",sError);
		Database.Connect(SQL_Connection, "db");
		return;
	}
	//Anything else to run for a successful query
	return;
}
You may also want to have a maximum attempts to connect and possibly a failover to sourcemod-local database with a retry after some amount of time so it doesn't loop forever if the database is down.
Balimbanana is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 05-02-2020 , 14:43   Re: Database reconnect on lost connection?
Reply With Quote #3

I do it on OnConfigsExecuted(), but it never helped me to reconnect (even after 1 min.), dunno why.

https://forums.alliedmods.net/showthread.php?t=320326
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 05-04-2020 , 12:15   Re: Database reconnect on lost connection?
Reply With Quote #4

Shouldn't sql connections work temporary ? Not leave connection open whole time ?

You open connection, do your stuff, close connection.
__________________
Do not Private Message @me
Bacardi is offline
zipcore
Veteran Member
Join Date: Mar 2010
Location: m_flZipcore
Old 05-06-2020 , 10:37   Re: Database reconnect on lost connection?
Reply With Quote #5

I don't see any reason why zou should close such connections just to reopen them later when they are using within a plugin which isn't sending a single query once. It only consumes a bit more memory on you database server which is only a problem when you don't have enough RAM on there. It isn't even unsecure to leave those connections alive.

But I don't see the point of reconnecting to a database on errors or losing the handle. I can't remember a single incidence where this would have helped. Useally you lose connection when a query fails and it needs to be fixed then anyways.
__________________

Last edited by zipcore; 05-06-2020 at 10:38.
zipcore is offline
Reply



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 12:08.


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