与hikari CP的连接泄漏



我有用于sql for sql语句的jdbs模板,并在多次调用后使用hikari连接池我不同意,我使用了资源尝试,我的错误在哪里?(Servlet容器-Tomcat(

public class SimpleJdbcTemplate {
private Connection connection;
private DataSource dataSource;
private ResultSet resultSet;
PreparedStatement preparedStatement;
public SimpleJdbcTemplate(Connection connection) {
    this.connection = connection;
}
//private DataSourse datasource - из hikariconnectionpool
public SimpleJdbcTemplate(DataSource dataSource) {
    this.dataSource = dataSource;
}
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) {
    try (PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement(sql)){
        resultSet = null;
        List<T> result = new ArrayList<>();
                                                         
        int position = 1;
        for (Object arg : args) {
            preparedStatement.setObject(position, arg);
            position++;
        }
        if (sql.contains("UPDATE") || sql.contains("update") ||sql.toLowerCase().contains("delete")||sql.toLowerCase().contains("insert")) {
            preparedStatement.executeUpdate();
        } else{
            resultSet = preparedStatement.executeQuery();
            if (resultSet == null) {
                throw new SQLException("No resultsSet");
            }
            while (resultSet.next()) {
                result.add(rowMapper.mapRow(resultSet));
            }
        }
        log.info(result.toString());
        return result;
    } catch (SQLException e) {
        throw new IllegalStateException(e);

日志:

2020年10月24日13:46:16.633严重[http-nio-8080-exec-3]org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service((用于路径为[/LabWork_war]的上下文中的Servlet[servlets.view.MessendjerViewServlet]引发异常java.lang.IollegalStateException:java.sql.SQLTransientConnectionException:HikariPool-1-连接不可用,请求在30014ms后超时。位于repository.utill.SimpleJdbcTemplate.query(SimpleJdbc Template.java:52(位于repository.jdbc.UserRepositoryImpl.findUserByUUID(UserRepositoryImpl.java:84(服务中。UserService.findUser(UserService.java:71(位于servlets.filter.AuthFilter.doFilter(AuthFilter.java:53(网址:org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193(网址:org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166(网址:org.apache.catalina.core.StandardWrapperValv.invoke(StandardWrapperValve.java:202(网址:org.apache.catalina.core.StandardContextValv.invoke(StandardContextValv.java:96(网址:org.apache.catalina.authenticator.AuthenticatorBase.ioke(AuthenticatorBase.java:541(网址:org.apache.catalina.core.StandardHostValv.invoke(StandardHostValv.java:139(网址:org.apache.catalina.vals.ErrorReportValve.ioke(ErrorReportValve.java:92(网址:org.apache.catalina.vals.AbstractAccessLogValv.invoke(AbstractAccessLogValve.java:690(网址:org.apache.catalina.core.StandardEngineValv.invoke(StandardEngineValv.java:74(网址:org.apache.catalina.connecter.CoyoteAdapter.service(Coyotedapter.java:343(网址:org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373(网址:org.apache.coyote.AbstractProcessorLight.produce(AbstractProcessorLight.java:65(网址:org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868(网址:org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589(网址:org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49(位于java.base/java.util.concurrent.ThreadPoolExecutiator.runWorker(ThreadPoolExecutiator.java:1130(位于java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630(网址:org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61(位于java.base/java.lang.Thread.run(Thread.java:832(由以下原因引起:java.sql.SQLTransientConnectionException:HikariPool-1-连接不可用,请求在30014ms后超时。访问com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:695(网址:com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:197(网址:com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:162(网址:com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:100(位于repository.utill.SimpleJdbcTemplate.query(SimpleJdbc Template.java:27(…还有22个

Okey我发现了一个错误,这一行不能正常工作(尝试使用资源((reparedStatement preparedStatement=dataSource.getConnection((.prepareStatement(sql((我想当我试图关闭连接时,它会从数据源(datasource.getConnection(((发出一个随机连接,所以,我的连接总是打开的,我将代码重新格式化为这个

public class SimpleJdbcTemplate {
DataSource dataSource;

//private DataSourse datasource - из hikariconnectionpool
public SimpleJdbcTemplate(DataSource dataSource) {
    this.dataSource = dataSource;
}
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) {  //TODO repair connection leak
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        connection = dataSource.getConnection();
        preparedStatement = connection.prepareStatement(sql);
        resultSet = null;
        List<T> result = new ArrayList<>();

        int position = 1;
        for (Object arg : args) {
            preparedStatement.setObject(position, arg);
            position++;
        }
        if (sql.contains("UPDATE") || sql.contains("update") || sql.toLowerCase().contains("delete") || sql.toLowerCase().contains("insert")) {
            preparedStatement.executeUpdate();
        } else {
            resultSet = preparedStatement.executeQuery();
            if (resultSet == null) {
                throw new SQLException("No resultsSet");
            }
            while (resultSet.next()) {
                result.add(rowMapper.mapRow(resultSet));
            }

        }
        log.info(result.toString());
        return result;
    } catch (SQLException e) {
        throw new IllegalStateException(e);
    } finally {
        try {
            if (resultSet != null) {
                resultSet.close();
                log.info("resultes finaly closed? - {}", resultSet.isClosed());
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        try {
            if (connection != null) {
                connection.close();
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
}

所有工作都正常

相关内容

  • 没有找到相关文章

最新更新