Mysql commands should be executed async?

Discussion in 'Plugin Development' started by Quaro, Jun 12, 2014.

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

    Quaro

    Mysql commands should be executed async? In-case im saving/loading large amount of data.
     
  2. Quaro Only do them async if you don't like server freezes. Otherwise it's fine. Also, the amount of data may be irrelevant - accessing a database can take time (i.e. latency). Just be sure not to use non-thread safe methods.
     
  3. Offline

    Quaro

    AdamQpzm Could you give little example with async?
     
  4. Offline

    rfsantos1996

    You can:
    Code:java
    1. private class HistoryExecutor implements CommandExecutor { // usage: "§6Use §c/history [player/pagina] [pagina]"
    2.  
    3. @Override
    4. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    5. if(sender.hasPermission("customserver.money.extract")) {
    6. int page = 1;
    7. String name;
    8. if(args.length > 1 && sender.hasPermission("customserver.money.extract.others")) {
    9. name = args[1];
    10. } else {
    11. name = sender.getName();
    12. }
    13. if(args.length > 0) {
    14. if(NumberUtils.isNumber(args[0])) {
    15. page = Integer.parseInt(args[0]);
    16. }
    17. if(page <= 0) { // can't be -1, must page -1 >= 0
    18. sender.sendMessage(Util.getLang("money.invalidValue"));
    19. return true;
    20. }
    21. }
    22. final int pages = page - 1; // 1 -> 0 // 2 -> 1
    23. new BukkitRunnable() { // after getting the necessary variables, and before executing MySQL things, add a runnable to do that Async
    24. @Override
    25. public void run() {
    26. if(CustomServer.getConfiguration().sql.hasPlayedBefore(name)) { // here you can use MySQL without locking the thread (;
    27. Collection<EconomyTransaction> moneyHistory = CustomServer.getConfiguration().sql.getMoneyHistory(name, pages);
    28. if(moneyHistory.size() > 0) {
    29. if(sender.hasPermission("customserver.money.extract.ip")) {
    30. sender.sendMessage(Util.getLang("money.extractTitleWithIp"));
    31. for(EconomyTransaction transaction : moneyHistory) {
    32. sender.sendMessage(Util.getLang("money.extractEntryWithIp")
    33. .replaceAll("%ip", transaction.getIp())
    34. .replaceAll("%date", Util.getFormatedDate(transaction.getTime()))
    35. .replaceAll("%type", transaction.getType().getDisplayName())
    36. .replaceAll("%amount", Util.formatMoney(transaction.getAmount()))
    37. .replaceAll("%to", transaction.getTo()));
    38. }
    39. } else {
    40. sender.sendMessage(Util.getLang("money.extractTitle"));
    41. for(EconomyTransaction transaction : moneyHistory) {
    42. sender.sendMessage(Util.getLang("money.extractEntry")
    43. .replaceAll("%date", Util.getFormatedDate(transaction.getTime()))
    44. .replaceAll("%type", transaction.getType().getDisplayName())
    45. .replaceAll("%amount", Util.formatMoney(transaction.getAmount()))
    46. .replaceAll("%to", transaction.getTo()));
    47. }
    48. }
    49. } else {
    50. sender.sendMessage(Util.getLang("money.historyNotFound"));
    51. }
    52. } else {
    53. sender.sendMessage(Util.getLang("playerNotFound"));
    54. }
    55. }
    56. }.runTaskAsynchronously(pl);
    57. return true;
    58. } else {
    59. sender.sendMessage(Util.getLang("noPermission"));
    60. return true;
    61. }
    62. }
    63. }

    So this way you will be able to run mySQL without locking the main thread, the only problem is delayed messages. OBS: do thins only with non-changing values, if you change a player money async, it may cause an "money from nowhere" effect.

    admin executed /money set Jaby 0 *Jaby spends money* the command is now executed async

    Tecnically he was out of money already, but the command was being executed by another thread. (I'm not saying that this is easy to happen, because this require luck and a perfect timing, but this CAN happen, and if your server have 200+ players, it'll happen for sure (lag from server + a lot of players doing a lot of stuff, the chance will grow)
     
Thread Status:
Not open for further replies.

Share This Page