Running mysql async

Discussion in 'Plugin Development' started by cfil360, May 4, 2015.

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

    cfil360

    I am trying to run my mysql connections asynchronously. I have done so successfully, however if you try to set the value and quickly get the value, the correct value hasn't been asynchronously retrieved, and retrieves the old value.
    Code:
    public static int value;
    
    public static void getStat(final UUID uuid, final String stat) {
            Bukkit.getServer().getScheduler()
                    .runTaskAsynchronously(HungerGames.getPlugin(), new Runnable() {
                        public void run() {
                            PreparedStatement statement;
                            try {
                                statement = connection.prepareStatement("SELECT kills FROM hungergames WHERE uuid = '6d8e0fca-5296-338d-b63b-dbbe0ecd51ed'");
                                ResultSet res = statement.executeQuery();
                                res.next();
                                value = res.getInt(stat);
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    });
        }
    
        public static int getStatValue(UUID uuid, String stat) {
            getStat(uuid, stat);
            return value;
        }
    
     
  2. Offline

    RawCode

    this is how concurrency works.

    or you expect your code to return new value before that value is set?
     
    Konato_K likes this.
  3. Offline

    cfil360

    @RawCode how can I fix this. Is there a way to make the method wait for the async task to finish?
     
  4. Offline

    mythbusterma

    @cfil360

    That would completely defeat the purpose of running it asynchronously. Just retrieve the value later.
     
    Konato_K likes this.
  5. Offline

    cfil360

    Say for example I have a money counter. I want to update the money. In order to update the money I retrieve the value then increase the value. I now want to output the value by sql retrieval. Instead of gathering the correct number it grabs the wrong one.
     
  6. Offline

    mythbusterma

    @cfil360

    First of all, you can do something like UPDATE ... SET kills = kills + 1 WHERE ... in SQL.

    Second we've already told you, you can't retrieve a value before it exists.

    Also, stop abusing the static modifier in that class, that's not what it's used for.
     
  7. Offline

    cfil360

    I have done the update. It was handled asynchronously also. When the method tried to retrieve the score afterwards to notify the player of their new balance. It was the wrong balance.
     
  8. Offline

    mythbusterma

    @cfil360

    That's probably because the update wasn't completed. Use a callback to notify the Player.
     
    cfil360 likes this.
  9. Offline

    RawCode

    bukkit\java\norse gods wont handle concurrency for you.

    if you working outside main thread (async) you must carefully join computation results with main thread on your own.

    just setting callback is not sufficient.
     
  10. Offline

    cfil360

    So can i see some examples of how this is done?
     
  11. Offline

    RawCode

Thread Status:
Not open for further replies.

Share This Page