Solved Mysql Communication faillure

Discussion in 'Plugin Development' started by guitargun, Jul 21, 2014.

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


    so my plugin makes connection to the mysql database of the server. here is the problem. on the minecraft server (not my local hosted) the mysql connection times out after some seconds and I can't reconnect. the only way to reconnect is by reloading the server. now when I local host it the error doesn't appear and everything works just fine. does someone have any idea's how to fix this?

    the error
    Communications link failure
    The last packet successfully received from the server was 76,814 milliseconds ago.  The last packet sent successfully to the server was 11 milliseconds ago.

    the connection code
    1. MySQL MySQl = database.MySQl;
    2. int poging = 0;
    3. while(con == null){
    4. con = MySQl.openConnection();
    5. if(con == null){
    6. poging++;
    7. getLogger().info("Database connection lost. Reconection will be started");
    9. }
    10. if(poging == 2){
    11. getLogger().info("No Connection to Database. Plugin is deactivating. Reload server for database Connection");
    12. Bukkit.getPluginManager().disablePlugin(plugin);
    13. break;
    14. }
    16. }
    17. if(con != null){
    18. database.setcon(con);
    19. getLogger().info("Databse connection has succeed");
    20. database.TableCreate();

    database class methods

    1. public void intvar(){
    2. mysqlpass = plugin.getConfig().getString("mysqlpass");
    3. mysqluser = plugin.getConfig().getString("mysqluser");
    4. mysqldb = plugin.getConfig().getString("mysqldb");
    5. mysqlpot = plugin.getConfig().getString("mysqlport");
    6. mysqlhost = plugin.getConfig().getString("mysqlhost");
    7. MySQl = new MySQL(plugin, mysqlhost, mysqlpot, mysqldb, mysqluser, mysqlpass);
    8. }
    10. public DbStuff(Main instance){
    11. plugin = instance;
    12. }
    13. public void setcon(Connection connect){
    14. con = connect;
    15. }

    and final I use Husky's mysql database library if you need to know
  2. Offline



    This is an inherent part of MySQL and one of the reasons people recommend using a connection pool. You either need to send dummy SQL as sort of a "keep-alive" or you need to recreate the connection each time you use it.
    guitargun likes this.
  3. The database you are using on the minecraft server is closing the connection after an idle timer expires (in this case, I'd guess maybe it's set at 60 seconds? That would seem to be sane inside of 76 seconds between your plugin last querying the db).

    So, you have two ways to deal with this, I recommend implementing both.

    1. Have a keep alive query running every x seconds, where x is < the connection timeout period. This would be simply querying the database with something like "SELECT1;" on a repeating task. Do not go for something stupid like 1-5 seconds, that's just dumb and will chew up resources.
    2. Catch the connection failure, and reopen/rebuild related connection objects. I have no clue how you do that in that library, you might have to create a new instance of whatever class you use to connect to the database (in plain old JDBC, you have to make new connection and statement objects.)
    guitargun and mythbusterma like this.
  4. Offline


    mythbusterma, tehbeard
    I think I solved it for now. with some trials and errors I found that the connection closing is between 20 and 30 seconds, 25 seoncds just works.

    I did this. these 2 methods are called everytime I do something with the databse (that is only one method below and in one other class)
    1. private int scheduler;
    2. public void closecon(){
    3. int timeout = 25;
    6. int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
    7. public void run(){
    8. try{
    9. pst = con.prepareStatement("SELECT id FROM `User_Table` LIMIT 1;");
    10. pst.execute();
    11. }catch(SQLException ex){
    12. System.out.println(ex.getMessage());
    13. }
    14. }
    15. }, 0L, timeout *20L);
    16. scheduler = id;
    17. }
    18. public void opencon(){
    19. Bukkit.getScheduler().cancelTask(scheduler);
    20. }
Thread Status:
Not open for further replies.

Share This Page