简体   繁体   中英

Not being able to connect to MySql database

I've had more problems with my database in the past: It could not always get connection. The database runs on a website (webhosting), and I try to access it from my own PC. Here things go wrong, if I access it from localhost to localhost then it works okay.

Error that I get in Java: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

I got no clue why, I'm using similar structure as ever, namely the following:

public class SQL {
    private final static String USERNAME = "";
    private final static String PASSWORD = "";
    private final static String URL = "jdbc:mysql://www.fvheeswijk.nl:3306/p28004_bf4";

    private static Connection cachedConnection;

    private static void createConnection() {
        cachedConnection = null;
        Properties connectionProperties = new Properties();
        connectionProperties.put("user", USERNAME);
        connectionProperties.put("password", PASSWORD);
        try {
            cachedConnection = (Connection) DriverManager.getConnection(URL, connectionProperties);
        } catch (SQLException ex) {
            Logger.getLogger(SQL.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Connection getConnection() {
        if (cachedConnection == null) {
            createConnection();
        }
        return cachedConnection;
    }
}

The data is blanked out of course.

I then tried to ping my website, all fine.

Later I tried to ping www.fvheeswijk.nl:3306 , the database, but Windows cmd cannot find it. Then I tried visiting it via the browser (does this even make sense?), but I got some message along the lines of "packets received out of order". And I have already (way before) added my PC's (Router/Network's) host name to the allowed host list of the database.

Any clue or suggestions what is going wrong?

EDIT: Now I am getting this, might explain something... java.sql.SQLException: null, message from server: "Host '541DB0AA.cm-5-6c.dynamic.ziggo.nl' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"

The main problem is that you are opening too much connections and probably never closing them or they are being closed by the application server (or wherever you run this application). This can be known from two facts in your post:

  • private static Connection cachedConnection . The database connection must not be cached manually, instead it should be retrieved only when needed, and closed after being used.
  • com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. This error is very explicit, you're trying to use a connection that is closed.

Also, you're naively opening connections manually, this is noted here:

cachedConnection = (Connection) DriverManager.getConnection(URL, connectionProperties);

To solve all these problems, you should move to a database connection pool . In short, the connection pool will open a bunch of physical database connections and keep them alive in sleeping status, and will wake up a connection on demand, then when closing the connection, instead of closing the physical connection, it will be back to the sleeping status, thus saving time for opening a new connection.

More info:


About your last edit, seems that you need to close some connections to your database. You should kill some of them and try to connect again using the database pool instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM