Execute (SQL) query and return result from separate thread

Discussion in 'Plugin Development' started by Nogtail, Feb 9, 2014.

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

    Nogtail

    I would like to execute a SQL query and return the result from another thread without pausing execution on the main thread while waiting for the query to complete.
     
  2. Offline

    SuperOmegaCow

    Nogtail for retrieving data:
    Code:java
    1. public class ThreadDown implements Runnable {
    2.  
    3. public boolean running = false;
    4. String data = null;
    5. private PreparedStatement sql = null;
    6. private ResultSet res = null;
    7. private boolean contains = false;
    8. private Object[] processed;
    9.  
    10. public ThreadDown(String uuid) {
    11. Thread thread = new Thread(this);
    12. this.data = uuid;
    13. thread.start();
    14. }
    15.  
    16. @Override
    17. public void run() {
    18. this.running = true;
    19. Bukkit.getPluginManager().callEvent(new ThreadDownEvent(this.running, this, this.processed));
    20. try {
    21. sql = Main.getInstance().c.prepareStatement("SELECT * FROM `cubedmc`.`thecube` WHERE user_id=? LIMIT 1;");
    22. sql.setString(1, this.data);
    23. res = sql.executeQuery();
    24. if (res.next()) {
    25. this.contains = true;
    26. }
    27. sql.close();
    28. res.close();
    29. if (contains) {
    30. sql = Main.getInstance().c.prepareStatement("SELECT * FROM `cubedmc`.`thecube` WHERE user_id=?;");
    31. sql.setString(1, this.data);
    32. res = sql.executeQuery();
    33. res.next();
    34. Object[] s = {res.getString("user_id"), res.getInt("kills"), res.getInt("deaths"), res.getInt("games_played"), res.getInt("wins"), res.getInt("cubeits"), res.getBoolean("vip"), res.getString("bought_classes")};
    35. this.processed = s;
    36. } else {
    37. Object[] k = {this.data, 0, 0, 0, 0, 0, false, " "};
    38. this.processed = k;
    39. }
    40.  
    41. } catch (SQLException e) {
    42. e.printStackTrace();
    43. } catch (NullPointerException e) {
    44. e.printStackTrace();
    45. } finally {
    46. try {
    47. if (res != null) {
    48. res.close();
    49. }
    50. if (sql != null) {
    51. sql.close();
    52. }
    53. } catch (SQLException e) {
    54. e.printStackTrace();
    55. }
    56. this.running = false;
    57. Bukkit.getPluginManager().callEvent(new ThreadDownEvent(this.running, this, this.processed));
    58. }
    59. }


    Then just create a event like so:
    Code:java
    1. public class ThreadDownEvent extends Event {
    2.  
    3. private static HandlerList handlers = new HandlerList();
    4. private boolean running;
    5. private ThreadDown thread;
    6. private Object[] data;
    7.  
    8. public ThreadDownEvent(boolean running, ThreadDown thread, Object[] data) {
    9. this.running = running;
    10. this.thread = thread;
    11. this.data = data;
    12. }
    13.  
    14. public HandlerList getHandlers() {
    15. return handlers;
    16. }
    17.  
    18. public static HandlerList getHandlerList() {
    19. return handlers;
    20. }
    21.  
    22. public boolean getRunning() {
    23. return this.running;
    24. }
    25.  
    26. public ThreadDown getThread() {
    27. return this.thread;
    28. }
    29.  
    30. public Object[] getDataProcessed() { return this.data; }
    31.  
    32. }
    33.  


    Then listen to the event like so:
    Code:java
    1. @EventHandler
    2. public void onThreadDown(ThreadDownEvent event) {
    3. if (!event.getRunning()) {
    4. Object[] data = event.getDataProcessed();
    5. FileConfiguration x = this.getConfig();
    6. x.set("players." + data[0] + ".kills", data[1]);
    7. x.set("players." + data[0] + ".deaths", data[2]);
    8. x.set("players." + data[0] + ".games_played", data[3]);
    9. x.set("players." + data[0] + ".wins", data[4]);
    10. x.set("players." + data[0] + ".cubeits", data[5]);
    11. x.set("players." + data[0] + ".vip", data[6]);
    12. x.set("players." + data[0] + ".bought_classes", data[7]);
    13. this.saveConfig();
    14. Bukkit.getLogger().info("Finished processing " + data[0] + "'s data.");
    15. }
    16. }
     
Thread Status:
Not open for further replies.

Share This Page