Reputation: 113
In application start up , I am loading data source as shown below code -
@Bean(name = "dataSource")
public DataSource getDataSource() {
ComboPooledDataSource dataSource = new ComboPooledDataSource ();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/usersdb");
dataSource.setUsername("root");
dataSource.setPassword("secret");
return dataSource;
}
So here I would like to verify that whether the data source loaded properly, One possible way could be datasource.getConnection() before return but here I have to create unnecessary connection object which is having some high cost.
Please help me to find a way to verify that data source has been instantiated properly. I have heard something about validate query but not sure how it would work. please suggest.
Upvotes: 2
Views: 3394
Reputation: 1337
It is impossible to validate DataSource
without test connection.
First of all, you define bean with singleton scope by default so single DataSource
will be created with single check.
Secondly, do not worry about the creation cost of Connection
because you use PooledDataSource
. 'Pooled' means that pool of connections exists that is why physical connection once created can be used many times by the application.
The validate query (or test query) is used to check if existing Connection
was broken. See Configuring Connection Testing for more details.
Upvotes: 2