async database

Started by vito, Dec 29, 2016, 09:36 AM

Previous topic - Next topic

vito

Hi.

Is it any plugins to make async connection to database to work without lags for vc-mp server (even if database is on another server)?


KAKAN

Squirrel is not async, so, no. But sure, you can use libuv and make a async SQLite plugin :D
oh no

NewK

This is one of the many reasons why I choose the Java plugin. On the CTF server I have a thread pool that I use to hold several threads for different purposes. One of those purposes is database interaction. All interactions with the database are done asynchronously in a different thread. I guess it would be possible on Squirrel somehow if someone made a plugin/module for it.

vito

#3
Quote from: KAKAN on Dec 29, 2016, 10:10 AMSquirrel is not async, so, no.
Nah, programming language can't be a problem, we just need an event about received data from our database with result of query (of course we need a different process for database).
Quote from: NewK on Dec 29, 2016, 10:21 AMAll interactions with the database are done asynchronously in a different thread.
Yes, I do not want to make my vc-mp server to wait when query to db will be done.

NewK

#4
Can be easily done with the Java plugin. Here's a simple example using Java8 and on the onPlayerSpawn event:


@Override
    public void onPlayerSpawn(Player player) {
     new Thread(() -> {

            /*
                some heavy database operation with player data here.
                It should be noted that you can't use the server API in
                a different thread (things like server.sendClientMessage(), server.getPlayer(), player.getIP(), etc...)
             */
            try (SyncBlock synced = server.sync()) {
                /*
                    here, you sync the spawned thread with the main server thread,
                    this will be called when the heavy operation above finishes and
                    inside this block you can use the server API normally. For
                    example to send a message to the player telling him the operation
                    has finished. If you do anything with the player instance here, make
                    sure to check first if the player is still inside the server with
                    player.isValid()
                     
                 */

            } catch (Exception error) {
                error.printStackTrace();
            }

        }).start();
}
If you don't need to use the server API after the heavy operation is over, then you won't need that try-catch SyncBlock at all. Here's a function as an example from the CTF5.0 server when player stats are being saved:
public void updateStats(Account acc) {
        new Thread(() -> {
            AccountsFacade.updateStats(acc);
        }).start();
 }