使用@Persistence上下文时检测到连接泄漏

时间:2018-07-04 10:13:41

标签: hibernate spring-data-jpa hikaricp

偶尔我会看到明显的连接泄漏检测到错误。以下是实体管理器的定义方式。有人可以指出导致此问题的原因吗?

打击是数据源定义:

@Bean
public HikariDataSource dataSource() {
    HikariConfig config = new HikariConfig();     

    config.setConnectionTestQuery("SELECT 1 FROM DUAL"); 
    config.setPoolName("test-pool");
    config.setDriverClassName(databaseProperties.getConnection().getDriver());
    config.setJdbcUrl(databaseProperties.getConnection().getUrl());
    config.setUsername(databaseProperties.getConnection().getUsername());
    config.setPassword(databaseProperties.getConnection().getPassword());
    config.setMaximumPoolSize(50);
    config.setMinimumIdle(10);
    config.setLeakDetectionThreshold(2000);
    config.setIdleTimeout(600000);
    config.setMaxLifetime(60000);
    config.setRegisterMbeans(true);
    return new HikariDataSource(config);
}

EntitiyManager定义

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final Properties properties = new Properties();
    final LocalContainerEntityManagerFactoryBean lC = new LocalContainerEntityManagerFactoryBean();
    lC.setDataSource(dataSource());
    lC.setJpaVendorAdapter(hibernateJpaVendorAdapter());
    lC.setPackagesToScan(databaseProperties.getHibernate().getPackagesToScan());

    properties.setProperty("hibernate.hbm2ddl.auto", databaseProperties.getHibernate().getHbm2ddl());
    properties.setProperty("hibernate.dialect", databaseProperties.getHibernate().getDialect());
    properties.setProperty("hibernate.jdbc.batch_size", databaseProperties.getHibernate().getBatchSize());
    properties.setProperty("hibernate.default_schema", databaseProperties.getHibernate().getDefaultSchema());
    properties.setProperty("hibernate.show_sql", String.valueOf(databaseProperties.getHibernate().isShowSql()));
    properties.setProperty("hibernate.cache.use_second_level_cache", String.valueOf(databaseProperties.getHibernate().isUseSecondLevelCache()));
    properties.setProperty("hibernate.cache.use_query_cache", String.valueOf(databaseProperties.getHibernate().isUseQueryCache()));
    properties.setProperty("hibernate.cache.region.factory_class", databaseProperties.getHibernate().getCacheFactoryClass());
    properties.setProperty("javax.persistence.sharedCache.mode", databaseProperties.getHibernate().getSharedCacheMode());
    properties.setProperty("hibernate.connection.characterEncoding", databaseProperties.getHibernate().getCharacterEncoding());
    properties.setProperty("hibernate.connection.CharSet", databaseProperties.getHibernate().getCharSet());
    properties.setProperty("hibernate.connection.useUnicode", databaseProperties.getHibernate().getUseUnicode());
    properties.setProperty("hibernate.connection.defaultNChar", databaseProperties.getHibernate().getConnectionDefaultNChar());

    lC.setJpaProperties(properties);

    return lC;
}

以下是我使用持久性上下文的方式。

public class MyServiceImpl implements MyService{

@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;

@Override
public String isBranchOpen(long branchId) {
    Query q;
    q = entityManager.createNativeQuery("SELECT TEST_PKG.is_branch_open_f(?) from dual");
    q.setParameter(1,branchId);

    String result = (String) q.getSingleResult();
    return result;
}

}

0 个答案:

没有答案