Mixup in MySQL Querrys

Discussion in 'Plugin Development' started by L33m4n123, Dec 16, 2013.

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

    L33m4n123

    Hey guys,

    I hope my Thread title is not too much misleading. So. My issue is that the MySQL Querrys are getting mixed up if there are multiple people interacting with it at the same time/short timeframe

    What do I mean?

    They have an Inventory Menu showing them some Items. If they click one of them the Program makes a MySQL querry and printing out the result. [Works]

    However what I came across yesterday is that when multiple people are using it at the same time or at a very short timeframe it somehow mixes up and Player A gets his result. Player B gets Player A's result.

    So what I am doing is [narrowed quite a bit down but it is the same]

    Code:java
    1. @EventHandler
    2. public void onItemClick(InventoryClickEvent e) {
    3. if(e.getCurrentItem().getType().equals(Material.WOOL)) {
    4. Player p = (Player) e.getWhoClicked();
    5. p.sendMessage(String.valueOf(getInt(p, "wool")));
    6. }
    7. }
    8.  
    9.  
    10. public int getInt(Player p, String selection) {
    11. // here all the stuff to get the Integer from the MySQL Database
    12. // works perfectly fine if only one player at the time over a timeframe
    13. }


    So. Anyone has an Idea on how I can do it so it basicly always gets the right result?
    Was thinking of creating one big multidimensional Array at the start, as soon as the player joins add him in there with all the current value. But will most likely cause the same issue if multiple people join at once
     
  2. Offline

    1Rogue

    If the issue is multiple people accessing it, then just "synchronized" the method.

    Although it really shouldn't be an issue with multiple people accessing it. What is the code for getInt()?

    Also running an individual query for each person is inefficient, when you could simply do it in one query.
     
  3. simply use the keyword "synchronized"
     
  4. Offline

    L33m4n123

    Louis1234567890987654321 1Rogue

    Will test it with the synchronized, thanks

    and for the code

    Code:java
    1. Statement statement = conntection.createStatement();
    2. ResultSet rs = statement.executeQuery("SELECT `" +selection"` FROM `TestTable` WHERE `player`='"
    3. + p.getName() + "'");
    4. if(!(rs.next())) {
    5. return 0;
    6. }
    7. return rs.getInt(1);
     
  5. Offline

    LucasEmanuel

    If all of this is happening on the same thread, adding synchronized won't do anything.
     
  6. Offline

    1Rogue

    I wonder, where else are you accessing the connection variable?

    It would also help if you provide an actual error.
     
  7. Offline

    L33m4n123


    I am not getting any error. I just get the value from the wrong person. And thats the only time
     
  8. Offline

    NathanWolf

    Database connections should be ok with concurrency by nature, and as others have mentioned you're probably single-threaded so "synchronized" isn't going to do anything.

    Can we see your full code? It's possible there's a logic issues elsewhere that is getting the data mixed up.
     
Thread Status:
Not open for further replies.

Share This Page