aSync SQL question

Discussion in 'Plugin Development' started by CraftBang, Jun 19, 2014.

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

    CraftBang

    Okay so I have 2 codes, my question is will these both run aSync?
    About the first one im pretty sure:
    Code:
    public static void registerdefaultacc(final Player p){
    Bukkit.getScheduler().runTaskAsynchronously(Main.plugin, new Runnable(){
    public void run() {
    try{
    openConnection();
    PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `player_data` values(?,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,?);");
    newPlayer.setString(1, ""+p.getUniqueId());
    newPlayer.setString(2, "geen");
    newPlayer.execute();
    newPlayer.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    });
    }
    
    Second one :
    Code:
    public static void registerdefaultacc(final Player p){
    Bukkit.getScheduler().runTaskAsynchronously(Main.plugin, new Runnable(){
    public void run() {
    registeracc(p);
    }
    });
    }
    public static void registeracc(Player p){
    try{
    openConnection();
    PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `player_data` values(?,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,?);");
    newPlayer.setString(1, ""+p.getUniqueId());
    newPlayer.setString(2, "geen");
    newPlayer.execute();
    newPlayer.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    
    So you run the registerdefaultacc and it will do the void registeracc async.
    Is that true?
    If this works than I can fix a lot of problems with the async tasks.
    Since on some places it was impossible for me to use async, but if I can make a second method to run it async it would be easily fixed.

    Okay got my answer, first question answered thanks.


    SECOND QUESTION:
    Okay well so now I ran on this problem:
    I got this function to get a players clan:

    Code:
    public static String getClan(Player p, String what){
    openConnection();
    try{
    if(playerDataContainsPlayer(p)){
    PreparedStatement sql = connection.prepareStatement("SELECT " + what + " FROM `player_data` WHERE player=?");
    sql.setString(1, ""+p.getUniqueId());
    ResultSet result = sql.executeQuery();
    result.next();
    return(result.getString(what));
    }
    }catch (Exception e){
    e.printStackTrace();
    }
    return "geen";
    }
    
    Trying to make it async:
    Code:
     
    public static String aSyncgetClan(final Player p, final String what){
    Bukkit.getScheduler().runTaskAsynchronously(Main.plugin, new Runnable(){
    public void run() {
    getClan(p, what);//Code location X
    }
    });
    return "geen";
     
    }
    
    How do I get the returning string at Code location X ?

    Thanks in advance,
    CB
     
  2. Offline

    fireblast709

    CraftBang method registeracc is invoked from a thread that is not the main thread, thus it is async
     
    CraftBang likes this.
  3. Offline

    CraftBang

    fireblast709 I was also thinking something like that, just wanted to hear it from other people to control it :p
    Thanks a lot!
    This will make my life easier! :p

    Mhm is it maybe a good idea to share this for everyone who has problems with MySQL or SQL lag?

    Okay well so now I ran on this problem:
    I got this function to get a players clan:

    Code:
    public static String getClan(Player p, String what){
    openConnection();
    try{
    if(playerDataContainsPlayer(p)){
    PreparedStatement sql = connection.prepareStatement("SELECT " + what + " FROM `player_data` WHERE player=?");
    sql.setString(1, ""+p.getUniqueId());
    ResultSet result = sql.executeQuery();
    result.next();
    return(result.getString(what));
    }
    }catch (Exception e){
    e.printStackTrace();
    }
    return "geen";
    }
    
    Trying to make it async:
    Code:
     
    public static String aSyncgetClan(final Player p, final String what){
    Bukkit.getScheduler().runTaskAsynchronously(Main.plugin, new Runnable(){
    public void run() {
    getClan(p, what);//Code location X
    }
    });
    return "geen";
     
    }
    
    How do I get the returning string at Code location X ?

    Thanks in advance,
    CB

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    mythbusterma

    Look at a callable and future to do what you want: http://wiki.bukkit.org/Scheduler_Programming#Callable_and_Future. They will allow you to get a response from a different thread, alternatively you can call a synchronous Bukkit event from the asynchronous thread (yes, that is thread safe). Sorry, what I meant to say was use the scheduler to run a method on the main thread to fire the event.
     
  5. Offline

    CraftBang

    mythbusterma I'm trying to understand it, but I don't really understand it xD.
    Can you maybe give a little example ? (I'm not trying to get code feeded, just need a little start :p)
     
  6. Offline

    mythbusterma

    An example of a Callable/Future setup? Or an example of the scheduler/event setup?
     
  7. Offline

    CraftBang

    Well what do I need? I want to get à returning string from An async function?
     
  8. Offline

    CraftBang

  9. Offline

    mythbusterma

    Sorry, you need to make a new Callable, i.e. a class that implements Callable<?>, in your case you'd probably want Callable<String>. This is part of the normal Java API and you can check google for how to use it.

    You could also call BukkitScheduler#callSyncMethod(Plugin, Callable<T>) from the other thread, and directly run a method on the main thread with the callable.
     
Thread Status:
Not open for further replies.

Share This Page