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

SQL_ConnectCustom for threaded MySQL


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
busy.orange
Member
Join Date: May 2009
Old 08-19-2009 , 05:31   SQL_ConnectCustom for threaded MySQL
Reply With Quote #1

Using SQL_UnlockDatabase(g_hDatabase); without SQL_LockDatabase
can crash server?
What do you think about my code?
PHP Code:
public OnClientPostAdminCheck(client)
{
    if (
IsFakeClient(client)) return;
    if (
SQLConnect())//Try connect to DB
    
{
             
SQL_TQuery(g_hDatabaseT_IgnoreResults"bla bla");
    }
}
public 
T_IgnoreResults(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    if (
hndl == INVALID_HANDLE)
    {
        
LogError("Query failed! %s"error);
    } 

bool:SQLConnect()
{
    
// connect to the database if needed
    
if (g_hDatabase == INVALID_HANDLE)
    {
        new 
Handle:KV INVALID_HANDLE;            
        
KV=CreateKeyValues("pishpish");
        
KvSetString(KV,"driver","mysql");
        
KvSetString(KV,"host","remotehost");
        
KvSetString(KV,"database","DB");
        
KvSetString(KV,"user","user");
        
KvSetString(KV,"pass","pass");
        
KvSetString(KV,"port","3306");
        
decl String:error[255];
        
g_hDatabase SQL_ConnectCustom(KV,error,sizeof(error),true);
        if (
g_hDatabase != INVALID_HANDLE)
        {
            
SQL_UnlockDatabase(g_hDatabase);
            return 
true
        
}
        
LogMessage("Failed to connect to database, error: %s"error);
        return 
false;
    }
    else return 
true;

busy.orange is offline
recon0
Veteran Member
Join Date: Sep 2007
Location: US
Old 08-20-2009 , 00:31   Re: SQL_ConnectCustom for threaded MySQL
Reply With Quote #2

Let's apply the logic test: Why would you call UnlockDatabase before LockDatabase? Secondly, database locking is only used when mixing threaded and non threaded DB operations.
__________________

Last edited by recon0; 08-20-2009 at 00:35.
recon0 is offline
busy.orange
Member
Join Date: May 2009
Old 08-20-2009 , 04:39   Re: SQL_ConnectCustom for threaded MySQL
Reply With Quote #3

SQL_ConnectCustom is only non threaded
is that source i only connect via non threaded. but use only threaded mysql
busy.orange is offline
BAILOPAN
Join Date: Jan 2004
Old 08-25-2009 , 01:16   Re: SQL_ConnectCustom for threaded MySQL
Reply With Quote #4

There is an open bug on this, bug 3554.
__________________
egg
BAILOPAN is offline
busy.orange
Member
Join Date: May 2009
Old 08-25-2009 , 13:32   Re: SQL_ConnectCustom for threaded MySQL
Reply With Quote #5

PHP Code:
new Handle:hDatabase INVALID_HANDLE;
 
StartSQL()
{
    
SQL_TConnect(GotDatabase);
}
 
public 
GotDatabase(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    if (
hndl == INVALID_HANDLE)
    {
        
LogError("Database failure: %s"error);
    } else {
        
hDatabase hndl;
    }
}

CheckSteamID(userid, const String:auth[])
{
    
decl String:query[255];
    
Format(querysizeof(query), "SELECT userid FROM users WHERE steamid = '%s'"auth);
    
SQL_TQuery(hdatabaseT_CheckSteamIDqueryuserid)
}
 
public 
T_CheckSteamID(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    new 
client;
 
    
/* Make sure the client didn't disconnect while the thread was running */
    
if ((client GetClientOfUserId(data)) == 0)
    {
        return;
    }
 
    if (
hndl == INVALID_HANDLE)
    {
        
LogError("Query failed! %s"error);
        
KickClient(client"Authorization failed");
    } else if (!
SQL_GetRowCount(hndl)) {
        
KickClient(client"You are not a member");
    }

I whant CheckSteamID in OnClientPostAdminCheck.
but i have problem:
How proces new players, when have no DB connection?
busy.orange is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 08-25-2009 , 14:54   Re: SQL_ConnectCustom for threaded MySQL
Reply With Quote #6

When you don't have a database connection, you can't process any players of course. So make a database connection before you process them (in OnPluginStart).
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
busy.orange
Member
Join Date: May 2009
Old 08-26-2009 , 02:51   Re: SQL_ConnectCustom for threaded MySQL
Reply With Quote #7

DJ Tsunami
DB can be autodisconneced if it idle? (10-30 minutes)

How proces new players, when have no DB connection?
i mean how to connect DB(if need) before query
busy.orange is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 08-26-2009 , 05:00   Re: SQL_ConnectCustom for threaded MySQL
Reply With Quote #8

I've never seen the database auto-disconnect, you maintain the connection so you decide when it disconnects. Again you can't make a connection before the query, because the connection is threaded. You'll have to store the queries somewhere, connect, and then fire them afterwards (hoping they're not invalid anymore).
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami 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 00:25.


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