I am using DataSource and DataSourceTransactionManager spring beans and wiring them into JobRepository bean. Shouldn't one of these be lifecycle aware or have a close function to close the connection once my spring application is closing. My process is hanging unless I manually call DataSourceUtils.releaseConnection(...) before exiting. Am I missing something here? Is there some other bug in my code that can cause this?
Do I need to use a Connection Pool? How do I make spring to manage the connection lifecycle correctly.
@Bean
public DataSource dataSource(@Value("${batch_db.url}") String dataSourceUrl, AWSCredentials awsCredentials) {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(dataSourceUrl);
dataSource.setUsername(awsCredentials.getAWSAccessKeyId());
dataSource.setPassword(awsCredentials.getAWSSecretKey());
return dataSource;
}
@Bean
@DependsOn(value = "dataSource")
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
@Bean
@DependsOn(value = "dataSource")
public JobRepository jobRepository(DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception {
JobRepositoryFactoryBean jobRepository = new JobRepositoryFactoryBean();
jobRepository.setDataSource(dataSource);
jobRepository.setTransactionManager(transactionManager);
return jobRepository.getJobRepository();
}