在数据库之间循环导致未关闭的泄漏连接导致 'too many connections'

Cycling between databases results in leaked connections that do not close resulting in 'too many connections'

我目前正在研究在 mysql 数据库之间切换以检查某些更改的东西。然而,当使用下面的 switchSource 方法切换数据库和主机时,它会导致切换成功但连接保持打开状态并且似乎没有关闭或遵守 setMaxIdleTime 设置。

所以发生的事情是,每次它连接到数据库主机和数据库时,它都会创建更多连接,每次重新连接时这些连接都会不断累积,直到数据库主机停止接受连接并且 returns 'too many connections'错误。

我想知道如何最好地关闭这些连接。

当查询为 运行 时,它们处于 try catch 中(例如 try (Connection conn = DataSource.getInstance().getConnection()))并且语句也在 catch 之前关闭。因此,结合 setMaxIdleTime,我不确定为什么没有关闭连接。

如果有人能阐明这一点,我将不胜感激。感谢阅读。

package com.example.database;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.example.base.ShardNode;
import com.example.manage.ShardManager;
import com.mchange.v2.c3p0.ComboPooledDataSource;  

import static com.example.manage.ShardManager.shardNodeList;


// see https://www.javatips.net/blog/c3p0-connection-pooling-example
public class DataSource {

    private static DataSource datasource;
    private ComboPooledDataSource cpds;
    private static int currentShardId = -1;
    private static ShardNode currentShardNode;
    private static boolean dbIsNotSpecified;
    private static boolean clearConnections = false;

    private static String GetDatabaseUrlAndDB(ShardNode selectedShard) {
        System.out.println(selectedShard.getFullUrl() + ShardManager.getDatabaseName(currentShardId));

        if (dbIsNotSpecified) {
          return selectedShard.getFullUrl();
        }

        return selectedShard.getFullUrl() + ShardManager.getDatabaseName(currentShardId);
    }

    private DataSource() throws PropertyVetoException {

        this.cpds = new ComboPooledDataSource();
        this.cpds.setDriverClass("com.mysql.cj.jdbc.Driver");
        this.cpds.setJdbcUrl(GetDatabaseUrlAndDB(currentShardNode));
        this.cpds.setUser(currentShardNode.getUsername());
        this.cpds.setPassword(currentShardNode.getPassword());

        // the settings below are optional -- c3p0 can work with defaults
//        cpds.setInitialPoolSize(5);
//        cpds.setMinPoolSize(5);
//        cpds.setAcquireIncrement(5);
//        cpds.setMaxPoolSize(100);
//        cpds.setMaxStatements(100);

        /*
         * Set this low to prevent connections from hanging around after the worker has left
         * Otherwise it results in too many connections being made on a single node and the server
         * starts rejecting new connections.
         */
//        cpds.setMaxIdleTime(1);
    }

    /* Refreshes the datasource to use a new id */
    public static void switchSource(int shardId, boolean dbNotSpecified) throws PropertyVetoException {
        // TODO continue work here. Pass id to data source and pull through credentials
        currentShardId = shardId;
        dbIsNotSpecified = dbNotSpecified;

        for(ShardNode CurrentShard: shardNodeList) {
            if ((shardId >= CurrentShard.getStartingShard())
                    && (shardId <= CurrentShard.getEndingShard())) {
                currentShardNode = CurrentShard;
                datasource = new DataSource();

                break;
            }
        }

        if (datasource == null) {
            // Handle empty datasources
        }
    }

    public static DataSource getInstance() throws PropertyVetoException {
        /*
         * If the datasource is null the runner is likely to have
         * just been started so use the first shardNode in the list
         */
        if (datasource == null) {
            currentShardNode = shardNodeList.get(0);
            datasource = new DataSource();
        }

        return datasource;
    }

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

在像您这样的 long-lived 程序中,您必须 .close().getConnection() 获得的任何 Connection 对象,当您完成使用它时。如果不这样做,您将遇到问题中描述的连接泄漏。

您的数据源似乎支持池连接。这消除了 .getConnection() / .close().

重复循环对性能的影响