Save HashMap onDisable()

Discussion in 'Plugin Development' started by Staartvin, Aug 10, 2013.

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

    Staartvin

    I'm working on a plugin where I use a hashmap to store inventories.

    This hashmap gets saved on a MySQL database as well.

    I wanted it so when the servers stops, (my plugin's onDisable() function is called) all inventories are saved to the MySQL database. I save the inventories async.

    I tried to put my code in the onDisable() method, but it will split an error saying:

    "org.bukkit.plugin.IllegalPluginAccessException: Plugin attempted to register task while disabled". This is obviously true, but I need to save the hashmap as it will be lost when you restart/reload the server. Is there any way of saving to the database while the plugin is getting disabled?
     
  2. Offline

    Robotia

    The only feasible thing I can think of is to use onCommand with /stop, so:
    Code:java
    1. onCommand(blah blah)
    2. {
    3. if (command.equalsIgnoreCase("stop"))
    4. savemystuff;
    5. }
     
  3. Offline

    LinearLogic

    Unfortunately, that won't solve the issue if the update task is to be async. It also doesn't cover other plugins stopping/reloading the server.

    Don't. :p
    But I'm sure the thought has already crossed your mind and if it sufficed you'd be doing it, so here are a few suggestions:
    • Synchronous updates are only problematic if they are large. Perform incremental async updates of the database while the server is running (update every x minutes, or whenever x inventories have been changed) to reduce the potential size of a synchronous update, and perform said sync update onDisable.
    • Implement commands in your plugin to stop and reload the server: these commands run the async update and then perform the appropriate server operation. For instance, /safestop updates the database and then calls server.shutdown().
     
  4. Offline

    Staartvin

    Why should I not save it async? I've always been told to go async on MySQL as MySQL can block the main thread. Blocking the main thread even for a small amount of time is not fun for the server. ;)
     
  5. Offline

    LinearLogic

    You were correctly informed. Both techniques I suggested are ones I've used, and the first works fine for me because I perform my updates in such a way that guarantees the main thread won't be compromised. It's a little extra work, however, so perhaps my second suggestion will better suit your plugin.
     
  6. Offline

    Staartvin



    Thanks :)
     
  7. Offline

    dunbaratu

    Isn't it, generally speaking, a really bad idea to wait for the server to shut down nicely before you save your data to disk?

    I've lost lots of stuff on my server when the server crashes in a "not nice" way, just because so many plugins don't save their in-memory information to disk until a nice shutdown, and it's perfectly normal for a server to be running for days nonstop, which means lots of days of information not being saved.

    This doesn't help answer the question, I realize, but it's a big pet peeve of mine. I've had to remove plugins that I otherwise really liked, just for this reason alone. (One in particular let you store books inside bookshelves as a container, but then didn't save that inventory until server shutdown, meaning on a crash people lose ALL the work they put into writing the book because the book is now stored nowhere).
     
  8. Offline

    LinearLogic

    A very good point, and one of the reasons I suggested incremental backups. The two techniques could be easily combined to optimize shutdown time and retention of data in the event of a crash.
     
  9. Offline

    Staartvin


    Yes, this is true. I was planning on already having saves every x minutes. It came to my mind that when I use HashMaps, those will be reset on reload/restart. Some servers like to reload their server (which is really a bad way of restarting, but I cannot stop them from reloading), thus I need to save it when a server is stopped.
     
  10. Offline

    LinearLogic

    Not if you persist them. ;)
     
  11. Offline

    Staartvin

    Persisting? Never heard of it before. To the Google machine! ;)
     
Thread Status:
Not open for further replies.

Share This Page