Persistance + Scheduler = Bad News?

Discussion in 'Plugin Development' started by bob3695, Apr 22, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    bob3695

    Hello everyone,

    I am writing a plugin that uses persistance to store data that is needed by the plugin. I am also using the bukkit scheduler to do some processing that doesn't need done on the main thread so that way I try and keep it as efficient as possible. In the function that the scheduler calls I get some data from persistance and try and save data to it. When using a sqlite db (haven't tested with MySQL yet, but do have a connection working on my other computer, will test in a few days) I get a error stating that the database is locked. I believe it is because I am on two different threads trying to save data to the database. So my question to everyone is...has anyone successfully got persistance working on separate threads?

    I am also trying to think of ideas to cut down on how often I have to save/read data from the database which I might be able to avoid the above issue if I nail down a system for that but getting a system like that made will most likely take some time.
     
  2. Offline

    Raphfrk

    Not that it is much help, but you can use the callback feature with the scheduler.

    This allows you to call a method (or methods) on the main thread and then wait for a call-back.

    As a made up example. Assuming that you have a Player class and you want to figure out the name of the player. (I don't actually think this requires syncing, but let's assume it does).

    Code:
        String getPlayerName(final Player player) {
    
            Future<String> result = getServer().getScheduler().callSyncMethod(this, new Callable<String>() {
    
                @Override
                public String call() throws Exception {
                    return finalPlayer.getName();
                }
    
            });
    
            String playerName = null;
            try {
                playerName = result.get();
            } catch (InterruptedException e) {
    
            } catch (ExecutionException e) {
    
            }
        }
    
    The process is in 2 stages. First you submit the method call (as a Callable<Return Type>) and that gives you a Future as a result.

    If you then call result.get(), it will suspend the current thread until the main thread has executed your method.[/code]

    Also, you don't have to .get all the results one at a time.

    You could submit a few hundred functions and then .get them one after another.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
    ne0nx3r0 likes this.
  3. Offline

    bob3695

    Interesting. I actually went with a different approach. I built a cache type system and have a save function called by a scheduler and save any changed stuff. When any information is requested from the DB I save it to a variable and keep it for a set amount of time, if it isn't used again in that time it removes it from the cache. This seems to be working wonders and it cuts down on my DB calls for overused information in the DB.
     
Thread Status:
Not open for further replies.

Share This Page