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

Random mysql connection prob


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
JocAnis
Veteran Member
Join Date: Jun 2010
Old 08-22-2019 , 18:36   Random mysql connection prob
Reply With Quote #1

Im using mysql_init() with 0.1 sec task in plugin_init (tryed with 5.0 sec in there, also without 0.1 but insta making sql_makedbtuple)

mysqlit_init() is having:

Code:
g_SqlTuple = SQL_MakeDbTuple( "ip", "us", "pw", "db" )
new ErrorCode
SqlConnection = SQL_Connect( g_SqlTuple, ErrorCode, g_Error, charsmax( g_Error ) ) 
if( SqlConnection == Empty_Handle ) 
		set_fail_state( g_Error )
And on mapchange server wont be started in first 10sec or more...and when it goes Online, plugin with this sql connection is executing: set_fail_state...but important to mention is, this plugin is in 90% time working well...but totally random i got problems, like right now..error in logs is this, if it anyhow helps:

Code:
Plugin says: Can't connect to MySQL server on 'ip' (4)
.php with loading results from the same mysql is working without problems 100% time...also can log in phpmyadmin without problems in that database...amxmodx problem ? :/
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)

Last edited by JocAnis; 08-22-2019 at 18:37.
JocAnis is offline
thEsp
BANNED
Join Date: Aug 2017
Old 08-22-2019 , 19:50   Re: Random mysql connection prob
Reply With Quote #2

Do you close connection in plugin_end forward?
thEsp is offline
JocAnis
Veteran Member
Join Date: Jun 2010
Old 08-22-2019 , 20:13   Re: Random mysql connection prob
Reply With Quote #3

yep
Code:
public plugin_end() 
{ 
	SQL_FreeHandle( g_SqlTuple ) 
}
also no queries called on client_disconnected( id ) if it could make any problems...
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)

Last edited by JocAnis; 08-22-2019 at 20:13.
JocAnis is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 08-23-2019 , 00:54   Re: Random mysql connection prob
Reply With Quote #4

Can you show the full code?

Just to make things clear here you aren't closing the connection you are closing the tuple.
Quote:
yep
Code:
public plugin_end() 
{ 
	SQL_FreeHandle( g_SqlTuple ) 
}
Your connection will be closed with SQL_FreeHandle(SqlConnection);
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 08-23-2019 at 01:14.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
JocAnis
Veteran Member
Join Date: Jun 2010
Old 08-23-2019 , 03:35   Re: Random mysql connection prob
Reply With Quote #5

Thanks Natsheh, i changed that, unfortunately no changes...code is this:

PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
#include <geoip>
#include <colorchat>

#define PLUGIN "1241"
#define VERSION "1241"
#define AUTHOR "12421"

new Host[]     = "localhost" 
new User[]    = "root" 
new Pass[]     = "" 
new Db[]     = "db" 

new Handle:g_SqlTuple 
new g_Error[512
new 
Handle:SqlConnection

new g_iPlayerID33 ]
new 
bool:g_bLoaded33 ]

new 
hostuserpassdb

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
set_task0.2"MySql_Init" )

    
host    register_cvar"host""" )
    
user    register_cvar"user""" )
    
pass    register_cvar"pass""" )
    
db      register_cvar"db""" )
    
    
get_mapnameg_szMapcharsmaxg_szMap ) )
}
public 
MySql_Init() 

    new 
cvar_host50 ], cvar_user50 ], cvar_pass50 ], cvar_db50 ]
    
get_pcvar_stringhostcvar_hostcharsmaxcvar_host ) )
    
get_pcvar_stringusercvar_usercharsmaxcvar_user ) )
    
get_pcvar_stringpasscvar_passcharsmaxcvar_pass ) )
    
get_pcvar_stringdbcvar_dbcharsmaxcvar_db ) )
    
    
//g_SqlTuple = SQL_MakeDbTuple( cvar_host, cvar_user, cvar_pass, cvar_db ) 
    
g_SqlTuple SQL_MakeDbTuple"""""""" )
    new 
ErrorCode
    SqlConnection 
SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error)) 
    if(
SqlConnection == Empty_Handle
        
set_fail_state(g_Error
        
    new 
Handle:Queries 
    Queries 
SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS \
    BLABLA \
    )"
);
    
    if(!
SQL_Execute(Queries)) 
    { 
        
SQL_QueryError(Queries,g_Error,charsmax(g_Error)) 
        
set_fail_state(g_Error
        
    } 
    
SQL_FreeHandle(Queries
    
    
Queries SQL_PrepareQuery(SqlConnection"CREATE TABLE IF NOT EXISTS \
    BLABLA \
    )"
);
    
    if(!
SQL_Execute(Queries)) 
    { 
        
SQL_QueryError(Queries,g_Error,charsmax(g_Error)) 
        
set_fail_state(g_Error
        
    } 
    
SQL_FreeHandle(Queries
    
    
Queries SQL_PrepareQuery(SqlConnection"CREATE TABLE IF NOT EXISTS \
    BLABLA \
    )"
);
    
    if(!
SQL_Execute(Queries)) 
    { 
        
SQL_QueryError(Queries,g_Error,charsmax(g_Error)) 
        
set_fail_state(g_Error
        
    } 
    
SQL_FreeHandle(Queries
    
    
SQL_FreeHandle(SqlConnection
    
    
set_task0.3"handleThatMap" )
}
public 
handleThatMap()
{    
    new 
gszQuery512 ]
    
formatex(gszQuerycharsmax(gszQuery),"SELECT mid FROM `maps` WHERE mapname = '%s '"g_szMap )
    
SQL_ThreadQuery(g_SqlTuple"handleSelectMap"gszQuery);

public 
plugin_end() 

    
SQL_FreeHandleSqlConnection 

public 
handleSelectMap(failstateHandle:Queryerror[], errnumdata[], size)
{
    if(
failstate != TQUERY_SUCCESS){
        
log_amx("SQL Insert error: %s",error);
        return;
    }
    
    if(
SQL_NumRows(Query) < 1)
    {
        new 
gszQuery512 ]
        
formatex(gszQuerycharsmax(gszQuery), "INSERT INTO `primekz_maps` VALUES ( '0', '%s' )" g_szMap );
        
SQL_ThreadQuery(g_SqlTuple"IgnoreHandle"gszQuery);
        
        
//more correct results than sql_getinsertid
        
set_task0.2"handleThatMap" )
    }
    else
    {
        
g_iMID SQL_ReadResultQuery)
    }
    
}
public 
client_putinserverid )
{
    if( !
is_user_botid ) )
    {
        
set_task0.8"Load_MySql"id )
    }
}
public 
Load_MySql(id

    new 
szSteamId[32], szTemp[512
    
get_user_authid(idszSteamIdcharsmax(szSteamId)) 
    
    new 
Data[1
    
Data[0] = id 
    format
(szTemp,charsmax(szTemp),"SELECT * FROM players WHERE steamid = '%s'"szSteamId
    
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1

public 
register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(
FailState == TQUERY_CONNECT_FAILED)
    {
        
log_amx("Load - Could not connect to SQL database.  [%d] %s"ErrcodeError)
    }
    else if(
FailState == TQUERY_QUERY_FAILED)
    {
        
log_amx("Load Query failed. [%d] %s"ErrcodeError)
    }
    
    new 
id
    id 
Data[0]
    
    if(
SQL_NumResults(Query) < 1
    {
        new 
szSteamId[32]
        
get_user_authid(idszSteamIdcharsmax(szSteamId)) // get user's steamid
        
        
if (equal(szSteamId,"ID_PENDING") || containiszSteamId"BOT" ) != -)
            return 
PLUGIN_HANDLED
        
        
new szTemp512 ]
        new 
g_ip15 ], g_country40 ]
        new 
name64 ]
        
        
get_user_nameidnamecharsmaxname ) )
        
        
SQL_PrepareStringnamenamecharsmaxname ));
        
        
get_user_ipidg_ipcharsmaxg_ip ), )
        
geoip_country_ex(g_ipg_countrycharsmaxg_country ), -1)  

        new 
szCode]
        
        
getNationalityidg_ipszCode);
        
        
format(szTemp,charsmax(szTemp),"INSERT INTO players BLABLABLBLA"blablabla )
        
        
SQL_ThreadQueryg_SqlTuple"IgnoreHandle"szTemp )
        
        
formatexg_playerCountryid ], charsmaxg_playerCountry[] ), g_country )
        
        
//sql_getinsertID - never use again:
        
set_task0.4"Load_MySql"id )
        
    } 
    else 
    {
        
//player loaded
        
BLABLA...
    }
    
g_bLoadedid ] = true

    
return PLUGIN_HANDLED
}  
getNationality(id, const szIP[], szCode[5]){
    new 
szTemp[3];
    if(
geoip_code2_exszIPszTemp)){
        
copy(szCode4szTemp);
    }
    else{
        
get_user_info(id"lang"szCode2);
        
SQL_PrepareString(szCodeszCode4);
    }
}
stock SQL_PrepareString(const szQuery[], szOutPut[], size)
{
    
copy(szOutPutsizeszQuery);
    
replace_all(szOutPutsize"'""\'");
    
replace_all(szOutPut,size"`""\`");
    
replace_all(szOutPut,size"\\""\\\\");
}
public 
IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize

    
SQL_FreeHandle(Query

    return 
PLUGIN_HANDLED 

im skipping cvars so adding directly connect info in:
g_SqlTuple = SQL_MakeDbTuple( "", "", "", "" )

all that happens before load_mysql being called (also handleThatMap)...so mysql_init...

im still confused how it worked first 3-4 days without errors, but now it got stuck for more than 12 hours..im starting to think that cs cant go with mysql, to work 100%..i hope im not right with that opinion...ps: queries are called only on mapstart, player connect, and when player finish the map (~every minute) so not so much going on to the mysql
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)
JocAnis is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 08-23-2019 , 06:19   Re: Random mysql connection prob
Reply With Quote #6

You have many mistakes for example you should call the mySql_Init function after registering the cvars and I don't see why you are creating the connection early if you are retrieving it's info by cvars, cvars are read in cfg files in plugin_cfg forward which after plugin init been called.

So you need to register the cvars in plugin init
(Call Mysql_init / connect to mysql ) in plugin_cfg forward.

Also you don't have to free the query in IgnoreHandle

You also should free the tuple in the plugin end forward

I'll give you more info soon.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
JocAnis
Veteran Member
Join Date: Jun 2010
Old 08-23-2019 , 07:55   Re: Random mysql connection prob
Reply With Quote #7

update: its now working, i just removed one more plugin which is connecting to the same mysql database on mapstart (but it was disabled 12 hours ago, but oke)

Thanks @Natsheh, ye i somehow didnt think about timing for cvars...so on plugin_end i should call both closing, first tuple, then connection?

about ignorehandle, should i even need that registered as public func, if it will stay empty?
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)
JocAnis is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 08-23-2019 , 08:12   Re: Random mysql connection prob
Reply With Quote #8

You have to close the connection, tuple is optional.
__________________
HamletEagle is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 08-23-2019 , 08:26   Re: Random mysql connection prob
Reply With Quote #9

Lmao youre actually closing the connection in mysql_init so closing the connection also in plugin end forward will result in error because the connection is already closed.


Also IgnoreHandle is nesscary for SQL_ThreadQuery to get back the result even if there is no results it expect a function to call.

Unless it got updated in amxz latest version for the callbackfunction to be ignored.

Also I d recommend using SQL_ThreadQuery most times since it filters the sql query by order against lag.


Make sure also to end each mysql statement/query with a semicolon ;
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 08-23-2019 at 08:36.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
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 20:29.


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