简体   繁体   English

连接到MySQL数据库Java

[英]Connecting to MySQL database java

I'm using some code for multi threaded MySQL system. 我正在为多线程MySQL系统使用一些代码。 It's working fine and will put entry's into my database in under half a second. 它工作正常,将在半秒钟内将条目的内容放入我的数据库。 My problem is, the rate at which my program will successfully connect and write to the database, is not great. 我的问题是,程序成功连接并写入数据库的速率并不高。 Some of the time it will work fine and other times I'll receive this error, or similar: 有时它会正常工作,而有时我会收到此错误,或类似的错误:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 27,697 milliseconds ago.  The last packet sent successfully to the server was 1 milliseconds ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3670)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3559)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4110)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
    at MySQL.putEntry(MySQL.java:46)
    at MySQL.main(MySQL.java:105)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3119)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3570)
    ... 9 more

I'm really not sure why this happens? 我真的不确定为什么会这样吗? Executing too many statements from one address?? 从一个地址执行太多语句? Anywho, here's my code: 任何人,这是我的代码:

private final ThreadedSQL sql;
private final DatabaseConnection connection;
private PreparedStatement statement;

public Hiscores() {
    try {
        loadConfig();
    } catch (IOException e) {
        e.printStackTrace();
    }
    sql = new ThreadedSQL(config);
    connection = sql.getConnectionPool().nextFree();
}

public void putEntry(final Player player) {
    try {
        statement = connection.getConnection().prepareStatement(QUERY(player));
        statement.execute();
        statement.closeOnCompletion();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

The code I've not included is irrelevant, uses java.sql.Connection. 我未包含的代码无关紧要,使用java.sql.Connection。 Does anyone have any idea why this is occuring? 有谁知道为什么会这样吗? Thanks. 谢谢。

You should close your statement and connection, once you are done, otherwise resource pool may be exhausted or your connection may time out. 完成后,应关闭语句和连接,否则资源池可能已耗尽或连接可能超时。 I recommend to obtain new connection from some JDBC pool. 我建议从某些JDBC池中获得新的连接。 I used to use proxool few years ago in servlets environment. 几年前,我曾经在servlets环境中使用过proxool。 If you are in J2EE, then application server shall do it for you, but you have to use DataSource. 如果您使用的是J2EE,则应用程序服务器将为您执行此操作,但是您必须使用DataSource。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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