MYSQL - 通信链路故障;超时

时间:2014-08-23 02:55:02

标签: java mysql sql

下面的类创建了一个与我的MYSQL用户的连接,然后它将一些数据提交到一个表中..我已经检查并仔细检查了用户名和密码,它肯定100%正确,但为什么它会继续抛出这个内存泄漏?我自己很难搞清楚这一点,我一直在阅读所有相关主题,但似乎没有一个与我的相同。

package com.rs.MYSQL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import com.rs.Settings;
import com.rs.utils.Lottery;

public class LotteryMYSQL {

    private static Connection connection;

    private static long lastConnection = System.currentTimeMillis();

    public static void createConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
                    "jdbc:mysql://runerebellion.com:3306/database",
                    "user", "pass");//fake user&pass
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void destroyConnection() {
        try {
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void init() {
        createConnection();
    }

    public static boolean updateInfo() {
        try {
            if (Settings.DisableMYSQL == true)
                return false;
            createConnection();
            Statement stmt = connection.createStatement();
            if (Lottery.getCurrentLotteryWinner() != null)
                stmt.executeUpdate("UPDATE info SET moneyEarned = '"
                        + Lottery.options.size() + "', lastWinner = '"
                        + Lottery.getCurrentLotteryWinner() + "'");
            else
                stmt.executeUpdate("UPDATE info SET moneyEarned = '"
                        + Lottery.options.size() + "', lastWinner = '"
                        + Lottery.getLastLotteryWinner() + "'");
            destroyConnection();
        } catch (Throwable e) {
            if (System.currentTimeMillis() - lastConnection > 10000) {
                destroyConnection();
                createConnection();
                lastConnection = System.currentTimeMillis();
            }
        }
        return false;
    }
}

内存泄漏低于

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

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    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:408)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1137)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:356)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2504)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2541)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2323)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    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:408)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.rs.MYSQL.LotteryMYSQL.createConnection(LotteryMYSQL.java:19)
    at com.rs.Launcher.main(Launcher.java:103)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:306)
    ... 16 more

1 个答案:

答案 0 :(得分:-1)

用过这个: 它运作良好

Class.forName("com.mysql.jdbc.Driver").newInstance();
            String IP = "";
            String DB = "";
            String User = "";
            String Pass = "";
            connection = DriverManager.getConnection("jdbc:mysql://" + IP + "/"
                    + DB, User, Pass);