vito

  • Sr. Member
  • Posts: 405
async database
« on December 29th, 2016, 09:36 AM »
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

  • Wiki Contributor
  • Posts: 3,343
Re: async database
« Reply #1, on December 29th, 2016, 10:10 AM »
Squirrel is not async, so, no. But sure, you can use libuv and make a async SQLite plugin :D
oh no

NewK

  • VC:MP Beta Tester
  • Posts: 186
Re: async database
« Reply #2, on December 29th, 2016, 10:21 AM »
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

  • Sr. Member
  • Posts: 405
Re: async database
« Reply #3, on December 29th, 2016, 10:55 AM »Last edited on December 29th, 2016, 10:57 AM
Quote from KAKAN on December 29th, 2016, 10:10 AM
Squirrel 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 December 29th, 2016, 10:21 AM
All 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

  • VC:MP Beta Tester
  • Posts: 186
Re: async database
« Reply #4, on December 29th, 2016, 02:01 PM »Last edited on December 29th, 2016, 07:21 PM
Can be easily done with the Java plugin. Here's a simple example using Java8 and on the onPlayerSpawn event:

Code: [Select]

@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:
Code: [Select]
public void updateStats(Account acc) {
        new Thread(() -> {
            AccountsFacade.updateStats(acc);
        }).start();
 }