jdbctemplate未关闭连接

7gyucuyw  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(214)

我有一些密码

@Bean("jdbc")
public JdbcTemplate jdbc(@Qualifier("dataSource") DataSource ds) {
    return new JdbcTemplate(ds);
}

@Bean("dataSource")
public DataSource dataSource(Environment environment) {
    JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
    String jndiName = environment.getProperty("dbconnection.jndiName", DEFAULT_DATASOURCE_JNDI_NAME);
    try {
        return dataSourceLookup.getDataSource(jndiName);
    } catch (DataSourceLookupFailureException e) {
        PGSimpleDataSource dataSource = new PGSimpleDataSource();
        dataSource.setDatabaseName(environment.getProperty("dbconnection.dbname"));
        dataSource.setURL(environment.getProperty("dbconnection.url"));
        dataSource.setUser(environment.getProperty("dbconnection.username"));
        dataSource.setPassword(environment.getProperty("dbconnection.password"));
        dataSource.setCurrentSchema(environment.getProperty("dbconnection.dbschema"));
        return dataSource;
    }
}

现在,在自定义存储库方法(只读)中使用jdbctemplate,返回列表对象

Connection connection = null;
    try {
        connection = jdbcTemplate.getDataSource().getConnection();
        return findByParamsJdbc(arg1, arg2, arg3,
                arg4, arg5,
                arg6, arg7, arg8, arg9,
                arg10, arg11, arg12, connection);
    } catch (Exception e) {
        if (Objects.nonNull(connection)) {
            connection.close();
        }
    } finally {
        if (Objects.nonNull(connection)) {
            if (!connection.isClosed()) {
                connection.close();
            }
        }
    }
    throw ex999();

在findbyparamsjdbc中有各种preparedstatement,“connection.createarrayof”和e.t.c。
一段时间后,应用程序崩溃,服务器cpu 99%被加载
内存转储显示jdbctemplate没有关闭连接,每次都会打开一个新的连接,为什么会发生这种情况?

类名|浅堆|保留堆|百分比

org.hibernate.internal.sessionfactoryimpl@0x5c438a130 | 136 | 74883816 | 29.72%|-org.hibernate.engine.query.spi.queryplancache@0x5c4c0b658 | 32 | 70764832 | 28.08%|-org.hibernate.jpa.event.spi.jpaintegrator@0x5c48177c8 | 24 | 362856 | 0.14%|-org.hibernate.engine.jdbc.env.internal.jdbc environmentimpl@0x5c48a2cc8 | 56 124293,208 | 0.12%|-org.hibernate.persister.entity.singletableentitypersister@0x5c43e3590 | 552 | 235176 | 0.09%|-org.hibernate.service.internal.sessionfactoryserviceregistryimpl@0x5c4812e80 | 56 | 182936 | 0.07%|-org.hibernate.persister.entity.singletableentitypersister@0x5c4535518 | 552 | 119,416 | 0.05%|-org.hibernate.persister.entity.singletableentitypersister@0x5c455d680 | 552 | 118736 | 0.05%|-org.hibernate.persister.entity.singletableentitypersister@0x5c45697d8 | 552 | 117576 | 0.05%

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题