java.sql.SQLNonTransientConnectionException:无法创建与数据库服务器的连接。尝试重新连接3次。放弃

时间:2018-09-05 17:36:11

标签: java mysql eclipse exception

这是一个Web应用程序,无法正常工作,所以我写了一个主要方法来查看 问题。

驱动程序位于Java构建路径->库->类路径中                                并在库-> Web App库(在Project Explorer中)和WEB-INF-> lib

罐子是mysql-connector-java-8.0.12。

我已经尝试将其添加到我的连接字符串中:

  

“ useUnicode = true&useJDBCCompliantTimezoneShift = true&useLegacyDatetimeCode = false&serverTimezone = UTC”

但是没有用。

以下代码出错

    public synchronized static void aggiungiCliente() throws SQLException 
{
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    String insertSQL = "insert into utente values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

    try {
        connection = ConnectionPool.getConnection();
        preparedStatement = connection.prepareStatement(insertSQL);
        preparedStatement.setString(1, "aa");  
        preparedStatement.setString(2,"aa");
        preparedStatement.setString(3, "aa"); 
        preparedStatement.setString(4, "aa"); 
        preparedStatement.setString(5,"aa"); 
        preparedStatement.setString(6, "aa");
        preparedStatement.setInt(7, 12);
        preparedStatement.setString(8,"aa"); 
        preparedStatement.setString(9, "aa"); 
        preparedStatement.setString(10,"aa");
        preparedStatement.setInt(11, 12); 
        preparedStatement.setInt(12, 12);
        preparedStatement.setDate(13, java.sql.Date.valueOf(java.time.LocalDate.now())); 
        preparedStatement.setString(14,"aa"); 
        preparedStatement.setString(15,"aa"); 
        preparedStatement.executeUpdate();
        connection.commit();
    } finally {
        try {
            if (preparedStatement != null)
                preparedStatement.close();
        } finally {
            ConnectionPool.releaseConnection(connection);
        }
    }
}


public static void main(String[] args) {

    try {
        aggiungiCliente();
    }
    catch(SQLException e) {
        System.out.println(e);
    }

}

这是连接池应用

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList; 
import java.util.List;

public class ConnectionPool {

static {
    freeDbConnections = new LinkedList<Connection>();
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("DB driver not found:"+ e.getMessage());
    } 
}

private static synchronized Connection createDBConnection() throws SQLException {

    Connection newConnection = null;

    String ip = "localhost";
    String port = "3306";
    String db = "code_omnia?autoReconnect=true&useSSL=false";
    String username = "root";
    String password = "Bonucci97";


    newConnection = DriverManager.getConnection("jdbc:mysql://"+ ip+":"+ port+"/"+db, username, password);
    newConnection.setAutoCommit(false);
    return newConnection;
}

public static synchronized Connection getConnection() throws SQLException {
    Connection connection;

    if (!freeDbConnections.isEmpty()) {
        connection = (Connection) freeDbConnections.get(0);
        freeDbConnections.remove(0);

        try {
            if (connection.isClosed())
                connection = getConnection();
        } catch (SQLException e) {
            connection.close();
            connection = getConnection();
        }
    } else {
        connection = createDBConnection();
    }

    return connection;
}

public static synchronized void releaseConnection(Connection connection) throws SQLException {
    if (connection != null)
        freeDbConnections.add(connection);
}





private static List<Connection> freeDbConnections;
}

和堆栈跟踪

  

java.sql.SQLNonTransientConnectionException:无法创建与数据库服务器的连接。尝试重新连接3次。放弃。
      在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
      在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
      在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
      在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
      在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
      在com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:902)
      在com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827)
      在com.mysql.cj.jdbc.ConnectionImpl。(ConnectionImpl.java:456)
      在com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
      在com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
      在java.sql / java.sql.DriverManager.getConnection(未知来源)
      在java.sql / java.sql.DriverManager.getConnection(未知来源)
      在model.ConnectionPool.createDBConnection(ConnectionPool.java:28)
      在model.ConnectionPool.getConnection(ConnectionPool.java:49)
      在model.Query.aggiungiCliente(Query.java:38)
      在model.Query.main(Query.java:20)
  引起原因:com.mysql.cj.exceptions.InvalidConnectionAttributeException:服务器时区值'ora legale Europa occidentale'无法识别或代表多个时区。如果要利用时区支持,必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更特定的时区值。
      在java.base / jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
      在java.base / jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(未知来源)
      在java.base / jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(未知来源)
      在java.base / java.lang.reflect.Constructor.newInstance(未知来源)
      在com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
      在com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
      在com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
      位于com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2236)
      在com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2260)
      在com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1314)
      在com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:865)
      ...另外10个

0 个答案:

没有答案