Spring中的所有连接都在Tomcat数据源池中处于活动状态

时间:2017-12-05 18:28:45

标签: spring postgresql tomcat spring-boot datasource

My Spring Boot应用程序使用JDBCTemplate将SQL查询发送到PostgreSQL数据库。似乎每次通过模板从池中获取连接时,都不会释放连接。活动连接数(datasource.primary.active)总是在增加。

在日志中,在使用JDBCTemplate的SQL Query之后,我可以看到:

DEBUG o.s.j.d.DataSourceUtils - Returning JDBC Connection to DataSource

但空闲连接的计数保持相同的值,并且活动连接的数量不会减少。达到最大值时,无法检索连接以执行查询。

所以,我认为没有返回到数据源池的连接,有什么想法吗?

这是使用Actuator获得的数据源配置:

  "dataSource": {
        "prefix": "spring.datasource.tomcat",
        "properties": {
            "connectionProperties": null,
            "propagateInterruptState": false,
            "validator": null,
            "useDisposableConnectionFacade": true,
            "defaultCatalog": null,
            "validationInterval": 3000,
            "jmxEnabled": true,
            "ignoreExceptionOnPreLoad": false,
            "logAbandoned": false,
            "commitOnReturn": false,
            "password": "******",
            "maxIdle": 100,
            "testWhileIdle": false,
            "removeAbandoned": false,
            "poolProperties": {
                "dbProperties": {
                    "user": "postgres",
                    "password": "******"
                },
                "url": "jdbc:postgresql://localhost:5432/tvir",
                "driverClassName": "org.postgresql.Driver",
                "defaultAutoCommit": null,
                "defaultReadOnly": null,
                "defaultTransactionIsolation": -1,
                "defaultCatalog": null,
                "connectionProperties": null,
                "initialSize": 10,
                "maxActive": 100,
                "maxIdle": 100,
                "minIdle": 10,
                "maxWait": 30000,
                "validationQuery": "SELECT 1",
                "validationQueryTimeout": -1,
                "validatorClassName": null,
                "validator": null,
                "testOnBorrow": true,
                "testOnReturn": false,
                "testWhileIdle": false,
                "timeBetweenEvictionRunsMillis": 5000,
                "numTestsPerEvictionRun": 0,
                "minEvictableIdleTimeMillis": 60000,
                "accessToUnderlyingConnectionAllowed": true,
                "removeAbandoned": false,
                "removeAbandonedTimeout": 60,
                "logAbandoned": false,
                "name": "Tomcat Connection Pool[1-574817798]",
                "password": "******",
                "username": "postgres",
                "validationInterval": 3000,
                "jmxEnabled": true,
                "initSQL": null,
                "testOnConnect": false,
                "jdbcInterceptors": null,
                "fairQueue": true,
                "useEquals": true,
                "abandonWhenPercentageFull": 0,
                "maxAge": 0,
                "useLock": false,
                "suspectTimeout": 0,
                "dataSource": null,
                "dataSourceJNDI": null,
                "alternateUsernameAllowed": false,
                "commitOnReturn": false,
                "rollbackOnReturn": false,
                "useDisposableConnectionFacade": true,
                "logValidationErrors": false,
                "propagateInterruptState": false,
                "ignoreExceptionOnPreLoad": false,
                "useStatementFacade": true
            },

用于查询db的代码:

JdbcTemplate jdbcTemplate = appCtx.getBean(JdbcTemplate.class);

ResultSet columns = jdbcTemplate.getDataSource().getConnection().getMetaData().getColumns(null, null, source.getTable().toLowerCase(), null);

String selectList = "";


while (columns.next())
{

    String colName = columns.getString("COLUMN_NAME");
    String colType = columns.getString("DATA_TYPE");


    if(!selectList.equals("")) {
        selectList += ", ";
    }


    if((""+java.sql.Types.INTEGER).equalsIgnoreCase(colType) || 
            (""+java.sql.Types.DOUBLE).equalsIgnoreCase(colType) ||
            (""+java.sql.Types.BIGINT).equalsIgnoreCase(colType) ||
            (""+java.sql.Types.FLOAT).equalsIgnoreCase(colType) ) {
        selectList += "SUM(" + colName + ")";
    } else {
        selectList += "MAX(" + colName + ")";
    }

    selectList += " AS "+colName;
}

            String sql = "SELECT "+selectList+" FROM "+source.getTable()+" "+
                            "WHERE "+source.getDateColumn()+" >= ? "+
                            "AND "+source.getDateColumn()+" <= ? ";


List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, Date.valueOf(startDate), Date.valueOf(endDate));

1 个答案:

答案 0 :(得分:0)

Spring boot允许您配置数据源的行为方式。 您将在official doc找到完整列表。 检查您的案例的以下属性:

spring.datasource.maxActive
spring.datasource.maxIdle

根据您使用的连接池,您还可以使用spring boot属性对其进行调整(一切都在文档中)。