Linked Questions

Popular Questions

Database Connections are open until http request is not complete

Asked by At

I have spring-boot application with JPA and Hibernate as ORM. I am using Hikaricp for connection pooling. So I have an api that fetch some records form the database apply some business logic and returns the result to client. Since the business logic have some heavy task(calling another api) to complete.

I was expecting the database connection should be return back to pool since records fetched successfully and nothing to do with that connection but the connection is still open (getting connection leak by HikariCP (leakDetectionThreshold=5000))

Hikari config.

hikari: idleTimeout: 10000 connectionTimeout: 60000 maximumPoolSize: 10 minimumIdle: 2 poolName: gor-srms leakDetectionThreshold: 5000

Database configuration class

@Configuration @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();
}

}

Repository class

public interface ServiceRequestDao extends Dao<MServiceRequest, Long> {
@Query(value = "Native query.....", nativeQuery = true)
Long getParentId(@Param("childId") Long childId);

}

Related Questions