Server Crashing??

Discussion in 'Spigot Plugin Development' started by Swedz, Jun 6, 2016.

  1. Whenever I connect to the server I get this error: http://pastebin.com/5c39JHwp
    And at line 127 in Core,
    Code (Text):
    String uuid = API.uuidString(event.getPlayer().getName());
    And for the uuidString() I'm doing:
    Code (Text):
    public static String uuidString(String player) throws Exception {
            String value = query("get","uuids","select uuid from uuids where name = '" + player + "';","uuid");
            return value;
        }
    And for query() I'm doing:
    Code (Text):
    public static String query(String type, String db, String query, String column) throws Exception {
            String value = null;
            Connection database = getConnection(db,"USERNAME","PASSWORD");
            if(type.equalsIgnoreCase("get")) {
                Statement st = database.createStatement();
                ResultSet rs = st.executeQuery(query);
                if(rs.next()) {
                    value = rs.getString(column);
                }
            } else if(type.equalsIgnoreCase("post")) {
                PreparedStatement posted = database.prepareStatement(query);
                posted.executeUpdate();
            }
            database.close();
            return value;
        }
    And finally, for getConnection():
    Code (Text):
    public static Connection getConnection(String db, String user, String pass) throws Exception {
            try {
                String driver = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://localhost:3306/" + db;
                Class.forName(driver);
                Connection conn = DriverManager.getConnection(url, user, pass);
                return conn;
            } catch(Exception e) {
                log.severe("There was an error while connecting to the database.");
            }
            return null;
        }
    Thank you for your help in advance! :)
     
  2. Make the database access to async task. This should help against "The server has stopped responding".

    But without more code I can't really help. I work with database access in some of my plugins too.
     
  3. How would I make the connection ASync?
     
  4. Make a new task like this

    Code (Java):
    plugin.getServer().getScheduler().runTaskAsynchronously(plugin, task)
    if you now get an error it will be posted in the console.
     
  5. Put this in my onEnable()? Or getConnection()? Or what?
     
  6. You put this in the section where you call your function to get access to the database.
     
  7. What should I do for the plugin variable, as well as the task variable?
     
  8. I'm still confused though, because this was all working fine just a couple hours ago.
     
  9. One reason could be an error to establish the connection to the database server. If this happens in the main thread of the server, the server freezed couple of seconds and then "booom". To "fix" this just make an async task. If now the async task freezed, the main thread of the server will not stopped.
     
  10. Could I make the ASync task in onEnable() and make it stay for all of the class?

    EDIT: I'm going to bed now, so I won't be responding for a while.
     
  11. Sure, you can make an ASync task in onEnable() also you can make a class which extends Runnable and there you can make all what you want to do with it. And then you can write "new MyRunnable" instead "new Runnable".

    If you need more help just wait for someone else. I'm going to sleep now.

    I wish you good luck ;-)
     
  12. What is happening is you are trying to access the database, and your entire server is pausing while it waits for the reply. So the server "did it break?" thread looks and sees the server is doing nothing at all, assumes it crashed, and throws a potato.

    Anything that will take longer than about 20 milliseconds needs to be async.
     
    • Like Like x 1
    • Agree Agree x 1
  13. Code (Text):
    public static Connection getConnection(String db, String user, String pass) throws Exception {
            try {
                String driver = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://localhost:3306/" + db;
                Class.forName(driver);
                Connection conn = null;
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            conn = DriverManager.getConnection(url, user, pass);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                return conn;
            } catch(Exception e) {
                log.severe("There was an error while connecting to the database.");
            }
            return null;
        }
    This is what I'm trying to do for my getConnection() now, but inside of the "try" Eclipse is giving me an error at "conn"
    Code (Text):
    Local variable conn defined in an enclosing scope must be final or effectively final
     
  14. Youre trying to initialize a variable in a different thread. You wil need to move it into the thread where you initialize it and make it final. Dont forget to initialize your variable directly, as you cannot reinitialize final variables.
     
  15. But then I can't return the value.
    Code (Text):
    public static Connection getConnection(String db, String user, String pass) throws Exception {
            try {
                String driver = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://localhost:3306/" + db;
                Class.forName(driver);
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            Connection conn = DriverManager.getConnection(url, user, pass);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                return conn;
            } catch(Exception e) {
                log.severe("There was an error while connecting to the database.");
            }
            return null;
        }
     
  16. Code (Text):
    public static Connection getConnection(String db, String user, String pass) {
            Connection conn = null;
            Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
                @Override
                public void run() {
                    try {
                        String driver = "com.mysql.jdbc.Driver";
                        String url = "jdbc:mysql://localhost:3306/" + db;
                        Class.forName(driver);
                        conn = DriverManager.getConnection(url, user, pass);
                    } catch(Exception e) {
                        log.severe("There was an error while connecting to the database.");
                    }
                }
            });
            return conn;
        }
    This is still giving the same error..
    Can you please edit my code to show me what I must do? I'm really confused on what specifically I should do..
    Sorry for the hassle, I'm just really dumb.