我有使用JPA和Hibernate作为ORM的spring-boot应用程序。我正在使用Hikaricp进行连接池。因此,我有一个api,可以从数据库中获取一些记录,并应用一些业务逻辑并将结果返回给客户端。由于业务逻辑需要完成繁重的任务(调用另一个api)。
我期望数据库连接应该返回到池中,因为成功获取了记录,并且与该连接无关,但是连接仍处于打开状态(HikariCP导致连接泄漏(leakDetectionThreshold = 5000))
Hikari配置。
hikari:
idleTimeout: 10000
connectionTimeout: 60000
maximumPoolSize: 10
minimumIdle: 2
poolName: gor-srms
leakDetectionThreshold: 5000
数据库配置类
@配置 @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef =“ srmsEntityManagerFactory”,transactionManagerRef =“ srmsTransactionManager”,basePackages = {“ com.gor.platform.srms.service.dao”})
public class SrmsDatabaseConfig {
@Autowired
private Environment environment;
@Autowired
JpaVendorAdapter jpaVendorAdapter;
@Bean(name = "srmsDataSource")
public DataSource dataSource() {
final HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName(environment.getProperty("srms.db.driver"));
dataSource.setJdbcUrl(environment.getProperty("srms.db.url"));
dataSource.setUsername(environment.getProperty("srms.db.username"));
dataSource.setPassword(environment.getProperty("srms.db.password"));
dataSource.setIdleTimeout(Long.valueOf(environment.getProperty("hikari.idleTimeout")));
dataSource.setConnectionTimeout(Long.valueOf(environment.getProperty("hikari.connectionTimeout")));
dataSource.setMaximumPoolSize(Integer.parseInt(environment.getProperty("hikari.maximumPoolSize")));
dataSource.setPoolName(environment.getProperty("hikari.poolName"));
dataSource.setMinimumIdle(Integer.parseInt(environment.getProperty("hikari.minimumIdle")));
dataSource.setLeakDetectionThreshold(Long.valueOf(environment.getProperty("hikari.leakDetectionThreshold")));
return dataSource;
}
@Bean(name = "srmsEntityManagerFactory")
public EntityManagerFactory entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource());
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan(environment.getProperty("srms.entitymanager.packagesToScan"));
lef.setPersistenceUnitName(environment.getProperty("srms.hibernate.unit_name"));
lef.afterPropertiesSet();
return lef.getObject();
}
@Bean(name = "srmsTransactionManager")
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}
@Bean(name = "srmsEntityManager")
public EntityManager entityManager() {
return entityManagerFactory().createEntityManager();
}
}
存储库类
public interface ServiceRequestDao extends Dao<MServiceRequest, Long> {
@Query(value = "Native query.....", nativeQuery = true)
Long getParentId(@Param("childId") Long childId);
}