Reputation: 55
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();
}
Upvotes: 0
Views: 1587
Reputation: 21493
Per the javadoc for DriverManagerDataSource
(http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/datasource/DriverManagerDataSource.html), that class blindly creates a new connection every time you a connection from it. From there, there is no additional management of the connections so it is up to you to manage them. If you want to have someone else manage the connections, you'll want to use a proper connection pool.
Upvotes: 1