Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连接关闭方法不起作用,太多客户端出错_Java_Spring_Spring Boot_Jdbc_C3p0 - Fatal编程技术网

Java 连接关闭方法不起作用,太多客户端出错

Java 连接关闭方法不起作用,太多客户端出错,java,spring,spring-boot,jdbc,c3p0,Java,Spring,Spring Boot,Jdbc,C3p0,为什么连接关闭在此代码中不起作用?我尝试在try()中创建连接,但在执行testDbConnection几次后,出现了太多连接错误,并且当我试图通过pgAdmin连接到数据库时,我看到连接的客户端太多。我如何解决它?为什么关闭连接不起作用 private List<DataSource> getDataSources() { connectionsNumber = 2; List<DataSource> dataSources = new

为什么连接关闭在此代码中不起作用?我尝试在try()中创建连接,但在执行testDbConnection几次后,出现了太多连接错误,并且当我试图通过pgAdmin连接到数据库时,我看到连接的客户端太多。我如何解决它?为什么关闭连接不起作用

 private List<DataSource> getDataSources() {
        connectionsNumber = 2;
        List<DataSource> dataSources = new ArrayList<>();
        for (int i = 1; i <= connectionsNumber; i++) {
            Connection connection;
            DataSource dataSource;
            String jdbcUrl = environment.getProperty(String.format("database%d.url", i));
            String user = environment.getProperty(String.format("database%d.username", i));
            String password = environment.getProperty(String.format("database%d.password", i));
            ComboPooledDataSource cpds = new ComboPooledDataSource();
            try {
                cpds.setDriverClass("org.postgresql.Driver");
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
            cpds.setJdbcUrl(jdbcUrl);
            cpds.setUser(user);
            cpds.setPassword(password);
            cpds.setMinPoolSize(3);
            cpds.setAcquireIncrement(5);
            cpds.setMaxPoolSize(20);
            cpds.setMaxIdleTime(1);
            cpds.setMaxConnectionAge(600);
            cpds.setMaxStatements(500);
            dataSource = cpds;
            dataSources.add(dataSource);
        }
        return dataSources;
    }
private List getDataSources(){
连接数=2;
List dataSources=new ArrayList();

对于(int i=1;i在我看来,当使用c3p0时,
connection.close()
并不是真正关闭连接,只需将其放回池中即可。如果要清理数据源,可以使用
DataSources.destroy(DataSource)

“太多的客户端”来自服务器,而不是客户端,它出现在打开时,而不是关闭时,它出现在打开时。您似乎只是在这里测试平台。解决方案:不要。您到底想在这里实现什么?您使用的是连接池,因此物理连接保持打开不应感到惊讶(这就是重点)。要么你需要减少池大小,要么你需要增加最大连接数。你的代码似乎是人为的,因此你想解释你真正的问题。另外,请阅读“使用资源进行尝试”,如果你使用“使用资源进行尝试”,你脆弱的
最终可以删除阻止的
。这样做没有实际意义hough。保持连接打开是连接池(如c3p0)的全部要点。这听起来更像是OP有XY问题。我认为他在测试中打开了太多的
数据源。正如你所说,这样做没有实际意义。只是一个解决方案。
public void testDbConnection() throws SQLException {
        String query = "select id from users;";
        Statement st = null;
        ResultSet rs = null;
        Connection connection = null;
        List<DataSource> dataSources = getDataSources();
        for (DataSource dataSource : dataSources) {
            try {
                connection = dataSource.getConnection();
                st = connection.createStatement();
                rs = st.executeQuery(query);
                while (rs.next()) {
                    System.out.println("Connection");
                }
            } finally {
                if (st != null) {
                    st.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (connection != null) {
                    connection.close();
                }
                st = null;
                rs = null;
                connection = null;
            }
        }
    }