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; }
@cfil360 That would completely defeat the purpose of running it asynchronously. Just retrieve the value later.
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.
@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.
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.
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.