Solved MySQL Error

Discussion in 'Plugin Development' started by zachoooo, Nov 4, 2012.

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

    zachoooo

    Code:
    public static void startUp() {
            try {
                mysql.open();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (!mysql.checkTable("TTT")) {
                mysql.createTable("CREATE TABLE TTT(ID int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (ID), Username varchar(255) NOT NULL, Rank varchar(255) NOT NULL, Money int(11) NOT NULL, Kills int(11) NOT NULL, Deaths int(11) NOT NULL, Rounds int(11) NOT NULL, Banned tinyint(1) NOT NULL, Kicks int(11) NOT NULL)");
                Bukkit.getLogger().info("Database initialized.");
            }
        }
       
        public static void shutdown() {
            mysql.close();
            Bukkit.getLogger().info("Database connection ended.");
        }
    Code:
    getServer().getScheduler().scheduleSyncRepeatingTask(this, new ConnectionRunnable(), 36000L, 36000L);
    Hey everyone, I am trying to make my database connection reset every 30 minutes, but I am getting this error when it executes this:
    Code:
    public class ConnectionRunnable implements Runnable {
     
        @Override
        public void run() {
            MySQLManager.shutdown();
            MySQLManager.startUp();
     
        }
     
    }
    Code:
    2012-11-04 09:27:51 [SEVERE] com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
    Anyone know how I can fix this?
     
  2. Offline

    Njol

    I guess that you'll have to recreate the 'mysql' variable as obviously it won't allow you to do anything after closing it, in particular re-opening it.
     
  3. Offline

    zachoooo

    Error appears to have stopped occurring... Well yay I guess xD
     
  4. Offline

    CubieX

    I don't think this is properly "solved" as you still do not know what is/was happening.

    The problem is, you are using a "try" statement to establish your connection (which is a good thing),
    but then outside of it, you use the mysql connection object to create a table, without checking if the connection is actually open now.

    You have to do at least something like this before using your mysql object:

    Code:java
    1.  
    2. if(mysql.isValid())
    3. {
    4. // do fancy sql stuff
    5. }
    6.  


    Or even better and highly recommended:
    Always use your mysql object INSIDE your try { } block.
    Because there could be a connection problem right after you successfully opened your connection.
    So your Plugin will not crash and you can handle the next steps.
     
Thread Status:
Not open for further replies.

Share This Page