java.sql.SQLException:连接已关闭[POOL-HikariCP]

时间:2021-01-21 22:48:38

Hello i have a problem with the connection, it is closing the connection while performing a Query. I don´t know what happens D: Here is the configuration:

您好我有连接问题,它正在执行查询时关闭连接。我不知道会发生什么D:这是配置:

private static HikariDataSource Hikari;
public static String ID_Usuario;

public void connectToDatabase() {       

    Hikari = new HikariDataSource();       
    Hikari.setDriverClassName("com.mysql.jdbc.Driver");
    Hikari.setJdbcUrl("jdbc:mysql://localhost:3306/bank");
    Hikari.setUsername("root");
    Hikari.setPassword(""); 
    Hikari.setMaximumPoolSize(5);
    Hikari.setConnectionTimeout(300000);
    Hikari.addDataSourceProperty("cachePrepStmts", "true");
    Hikari.addDataSourceProperty("prepStmtCacheSize", "250");
    Hikari.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    Hikari.setConnectionTestQuery("SELECT 1");
}

public HikariDataSource getHikari(){
    return Hikari;
}

Now, here is the class where i used the pool, First i recieved a connection from Hikari.getConnection(). Then i saved it in "connection"

现在,这里是我使用池的类,首先我收到了来自Hikari.getConnection()的连接。然后我把它保存在“连接”中

Pool HikariPool;
HikariDataSource Hikari;
Connection connection;

  public SQL() {
    initComponents();
  }

    public void initComponents(){
        HikariPool= new Pool();
        HikariPool.connectToDatabase();

    Hikari=HikariPool.getHikari();

    try{
       connection= Hikari.getConnection();
    }catch(SQLException e){
        e.printStackTrace();
    }
}

And i use the "connection" in validateLogin()

我在validateLogin()中使用“连接”

public int validateLogin(String nip){
   int validation=0;
   String SQL="SELECT * FROM bank.account WHERE No_Account='"+account+"'   
    AND NIP='"+nip+"'";
    try{
        Statement stm=Conexion.createStatement();
        ResultSet rs= stm.executeQuery(SQL);
...

At line " Statement stm=connection.createStatement();" an error ocurred

在“Statement stm = connection.createStatement();”行发生错误

it says :

它说 :

[AWT-EventQueue-0] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-0  
- is starting.
java.sql.SQLException: Connection is closed at   
com.zaxxer.hikari.pool.ProxyConnection$ClosedConnection$1.invoke
(ProxyConnection.java:468)
at com.sun.proxy.$Proxy0.createStatement(Unknown Source)...

Why is closing the connection?

为什么关闭连接?

1 个解决方案

#1


2  

The structure of your code is rather unclear to me. But I don't think that you want to allocate a Connection in initComponents(). You should be obtaining a Connection when you need to run a query, and then closing it to return it to the pool.

您的代码结构对我来说还不太清楚。但我不认为你想在initComponents()中分配一个Connection。您需要在需要运行查询时获取连接,然后关闭它以将其返回到池中。

Something like...

就像是...

public int validateLogin(String nip) {
   int validation=0;
   String SQL="SELECT * FROM bank.account WHERE No_Account='"+account+"'   
    AND NIP='"+nip+"'";
    try (Connection conn = SQL.getConnection();
         Statement stm = conn.createStatement()) {
       ResultSet rs = stm.executeQuery(SQL);
       ...
    }
    catch (SQLException e) {
       ...
    }

The "try with resources" will close the Connection and Statement automatically.

“try with resources”将自动关闭Connection和Statement。

And where SQL.getConnection() does something like:

SQL.getConnection()执行以下操作:

public Connection getConnection() throws SQLException {
   return Hikari.getConnection();
}

#1


2  

The structure of your code is rather unclear to me. But I don't think that you want to allocate a Connection in initComponents(). You should be obtaining a Connection when you need to run a query, and then closing it to return it to the pool.

您的代码结构对我来说还不太清楚。但我不认为你想在initComponents()中分配一个Connection。您需要在需要运行查询时获取连接,然后关闭它以将其返回到池中。

Something like...

就像是...

public int validateLogin(String nip) {
   int validation=0;
   String SQL="SELECT * FROM bank.account WHERE No_Account='"+account+"'   
    AND NIP='"+nip+"'";
    try (Connection conn = SQL.getConnection();
         Statement stm = conn.createStatement()) {
       ResultSet rs = stm.executeQuery(SQL);
       ...
    }
    catch (SQLException e) {
       ...
    }

The "try with resources" will close the Connection and Statement automatically.

“try with resources”将自动关闭Connection和Statement。

And where SQL.getConnection() does something like:

SQL.getConnection()执行以下操作:

public Connection getConnection() throws SQLException {
   return Hikari.getConnection();
}