Solved MySQL - Best way to query blocks placed and destroyed

Discussion in 'Plugin Development' started by xDeeKay, Jan 4, 2015.

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

    xDeeKay

    Hey guys. I've got a database logging blocks placed and destroyed directly from the BlockPlaceEvent and BlockBreakEvent, but it's rather laggy. Would it be recommended to store these events in a hashmap, and then apply them to the database after x amount of time, or on player logout?
     
  2. Offline

    mythbusterma

    @xDeeKay

    I would store them, and then write to the database every 5 minutes or so. Make sure you do the queries asynchronously to avoid severe lag.
     
  3. Offline

    xDeeKay

    @mythbusterma Thanks, so pretty much what I was thinking. Should I have a separate timer for each event, or 1 global timer that would handle writing everything? Also, by asynchronously, do you mean to use .runTaskAsynchronously in the scheduler?
     
  4. Offline

    mythbusterma

    @xDeeKay

    Yes, I would use a ConcurrentHashMap of the data you want to write, and write the data in a couple of queries every 5 minutes or so.
     
  5. Offline

    xDeeKay

    @mythbusterma I'm trying to also write a players hashmap data to the database when they logout, but this causes a bit of lag too. Is there some way I can do this asynchronously or on a different thread, like with the scheduler?

    Right now I'm just doing this:
    Code:
                        PreparedStatement brokenUpdate = Main.connection
                                .prepareStatement("UPDATE `player_stats` SET blocks_broken=? WHERE uuid=?;");
                        brokenUpdate.setInt(1, previousBlocksBroken + Main.statsBlockBreak.get(playerName));
                        brokenUpdate.setString(2, uuid);
                        brokenUpdate.executeUpdate();
    Main.statsBlockBreak being the Concurrent HashMap for block breaks.
     
  6. Offline

    leon3001

    @xDeeKay Just create a new Thread object and use your BukkitRunnable in the constructur. (BukkitRunnable implements Runnable so that should work) Then call the Thread#start() method on your thread.

    EDIT: Oh, better use a ScheduledExecutorService for repeating tasks.

    Ignore that, the two guys below know what they are talking about.
     
    Last edited: Jan 12, 2015
  7. Offline

    mythbusterma

    @leon3001

    Why would you make a new Thread (a fairly expensive operation) when Bukkit has plenty of perfectly nice threads already made up for you that you can run tasks on by utilising the Bukkit Scheduler?

    EDIT: Oh, better actually use resources available to you than unnecessarily make your own.
     
    leon3001 likes this.
  8. Offline

    1Rogue

    There's really no reason to create your own ScheduledExecutorService for sql queries. Bukkit's scheduler does the job just fine and it isn't time-critical to send them.
     
    leon3001 likes this.
  9. Offline

    xDeeKay

    @mythbusterma @1Rogue
    Awesome, this works perfectly with no ticks skipped at all. Thanks for the help fellas!
     
    mythbusterma likes this.
Thread Status:
Not open for further replies.

Share This Page