如何验证数据源是否有效并正确配置? [英] How to verify that data source is valid and configured properly?

查看:439
本文介绍了如何验证数据源是否有效并正确配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @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);
返回dataSource;





$ b所以在这里我想验证数据源是否正确加载
在返回之前,一种可能的方式可能是 datasource.getConnection(),但在这里我必须创建不必要的连接对象,这有很高的成本。



请帮助我找到一种方法来验证数据源是否已正确实例化。我听说过关于验证查询的一些信息,但不知道它是如何工作的。请建议。

解决方案

无需验证 DataSource 首先,默认情况下你定义了具有单例作用域的bean,所以单个 DataSource 将被创建为单核查。 p>

其次,不要担心 Connection 的创建成本,因为您使用 PooledDataSource 'Pooled'意味着存在连接池,这就是为什么物理连接一旦创建就可以被应用程序多次使用。



>验证查询(或测试查询)用于检查现有的连接是否损坏。有关更多详细信息,请参阅配置连接测试


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.

解决方案

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.

这篇关于如何验证数据源是否有效并正确配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆