java.sql.SQLException:连接已关闭[POOL-HikariCP] [英] java.sql.SQLException: Connection is closed [POOL-HikariCP]

查看:1520
本文介绍了java.sql.SQLException:连接已关闭[POOL-HikariCP]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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;
}

现在,这是我使用池的类,首先,我从Hikari.getConnection()接收了一个连接.然后我将其保存在连接"中

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

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();
    }
}

我在validateLogin()中使用连接"

And i use the "connection" in 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);
...

在语句stm = connection.createStatement();"行中发生错误

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

它说:

[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)...

为什么要关闭连接?

推荐答案

对我来说,代码的结构还不清楚.但是我不认为您想在initComponents()中分配一个Connection.当您需要运行查询时,应该获得一个Connection,然后将其关闭以将其返回到池中.

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.

类似...

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.

SQL.getConnection()的作用类似于:

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

这篇关于java.sql.SQLException:连接已关闭[POOL-HikariCP]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆