How to verify that data source is valid and configured properly?

為{幸葍}努か 提交于 2019-12-11 06:35:52

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/41105822/how-to-verify-that-data-source-is-valid-and-configured-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!