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

Change normal Query into Threaded Query


Post New Thread Reply   
 
Thread Tools Display Modes
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 10-01-2016 , 05:42   Re: Threaded Query not keeping Information
Reply With Quote #11

That is not remotely the problem and transactions (just being used to run the same queries) will not help. The "issue" is that SQL_TQuery is asynchronous (which is exactly the point of it, so the game server doesn't get blocked waiting on results), so you don't have the response from the query until the completion callback is called.

To serialise dependent threaded queries (generally), you need to make the next query from the callback of the preceding one. In your case, that means moving everything after SQL_TQuery(dbConn, SQL_CreateGang, query, client); into SQL_CreateGang. Obviously, you need to do all your gathering from the command and store it away (in the query works just fine) - don't forget the serial of the client who called the command and the ReplySource to use. You do not need to serialise the other two queries, as sQuery_UpdatePlayer doesn't use the result from sQuery_CreateGang. You haven't provided enough code to show you how to do this with your queries, but it is pretty simple.

There is a serious problem with this though with your existing queries: race conditions. If Cmd_CreateGang is called again after the SQL server has calculated the result set but before SQL_CreateGang is called, there is a chance it could return the same ID twice.

In your case, we can do better than general case by rewriting your queries and using a transaction to clean up the code a bit. Again, can't show you with your queries because you didn't provide them. But the gist of it is to:
  • Insert into the gang table with NULL as the ID.
  • Insert or update the players table, getting the new gang ID with LAST_INSERT_ID()
  • You may need to run a 3rd query to get the gang ID back from the DB, but this will be safely atomic (and you could store it in a session variable to start with to avoid querying the players table).

I've merged your threads together because this makes a lot more sense with the context... You've gone a bit overboard with 4 threads for what is basically the same thing you are working on.
__________________

Last edited by asherkin; 10-01-2016 at 06:02.
asherkin is offline
Phil25
AlliedModders Donor
Join Date: Feb 2015
Old 10-01-2016 , 11:51   Re: Change normal Query into Threaded Query
Reply With Quote #12

Woops, I haven't noticed that you're selecting something in the first query. If you're trying to get what you have to work, you'd need to chain them together and put the latter two as a transaction in the callback of the first query (assuming they're just updating and not selecting anything). IF you're trying to get what you have to work, though, but asherkin has provided a simpler solution.

Last edited by Phil25; 10-01-2016 at 11:52.
Phil25 is offline
SkinnyBruv
AlliedModders Donor
Join Date: Feb 2013
Location: Straya
Old 10-01-2016 , 22:30   Re: Change normal Query into Threaded Query
Reply With Quote #13

Updated the code on First Post!

Have a sus now

Will add any extra details u guys need aswell



Oi guys how do you store the value "client" and put it over to the next callback?
__________________
www.ancientgaming.net is my story.
Everything happens for a reason.
Nothing is random...
#Do you even AGN...

Last edited by SkinnyBruv; 10-02-2016 at 00:51.
SkinnyBruv is offline
Send a message via Skype™ to SkinnyBruv
xines
Veteran Member
Join Date: Aug 2013
Location: Denmark
Old 10-02-2016 , 09:20   Re: Change normal Query into Threaded Query
Reply With Quote #14

Quote:
Originally Posted by SkinnyBruv View Post
Oi guys how do you store the value "client" and put it over to the next callback?
Like this:

PHP Code:
SQL_TQuery(dbConnSQL_MyCallbackqueryGetClientUserId(client));

public 
void SQL_MyCallback(Handle ownerHandle hndl, const char[] errorany userid)
{
    
int client GetClientOfUserId(userid);
    if (
client == 0) return; //Stop if id = 0
    
if (hndl == null || strlen(error) > 0)
    {
        
LogError("[Mysql] Query failed! %s"error);
    }
    else
    {
        
//Do stuff
    
}

__________________
xines is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 10-02-2016 , 10:00   Re: Change normal Query into Threaded Query
Reply With Quote #15

Cleaner code would be

PHP Code:
public void SQL_MyCallback(Handle ownerHandle hndl, const char[] errorany userid

    
int client GetClientOfUserId(userid); 
    if (
client == 0) return; //Stop if id = 0 
    
if (hndl == null || strlen(error) > 0
    { 
        
LogError("[Mysql] Query failed! %s"error); 
        return;
    } 
    
//Do stuff 

Potato Uno is offline
xines
Veteran Member
Join Date: Aug 2013
Location: Denmark
Old 10-02-2016 , 10:07   Re: Change normal Query into Threaded Query
Reply With Quote #16

Quote:
Originally Posted by Potato Uno View Post
Cleaner code would be

PHP Code:
public void SQL_MyCallback(Handle ownerHandle hndl, const char[] errorany userid

    
int client GetClientOfUserId(userid); 
    if (
client == 0) return; //Stop if id = 0 
    
if (hndl == null || strlen(error) > 0
    { 
        
LogError("[Mysql] Query failed! %s"error); 
        return;
    } 
    
//Do stuff 

Yeah i see that else, was not required .
__________________
xines is offline
SkinnyBruv
AlliedModders Donor
Join Date: Feb 2013
Location: Straya
Old 10-03-2016 , 01:15   Re: Change normal Query into Threaded Query
Reply With Quote #17

Is there a way to call a threaded query before Cmd_CreateGang?

Or do u need to call the threaded query in Cmd_CreateGang and then Move onto another void


Also if i do the lot, it doesn't seem to work -_-
__________________
www.ancientgaming.net is my story.
Everything happens for a reason.
Nothing is random...
#Do you even AGN...

Last edited by SkinnyBruv; 10-03-2016 at 02:18.
SkinnyBruv is offline
Send a message via Skype™ to SkinnyBruv
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 10-03-2016 , 03:45   Re: Change normal Query into Threaded Query
Reply With Quote #18

You need to chain the database queries one after the other.
__________________
Neuro Toxin 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 01:57.


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