问题描述
- 功能需求:
使用druid数据库连接池实现 动态的配置数据源功能:IP、端口、用户名、密码都是用户页面手动输入,可以测试连接,保存数据源。 - 问题说明:
既然是用户自己配置的数据源,就无法避免输入错误,连接失败等情况。- 预期情况:用户输入的配置错误,测试连接时,会返回连接失败的信息。
- 实际情况:数据源测试连接,连接失败后:
- 后台一直打印错误信息,一直自动重连
- 方法被阻塞无返回信息,导致前端页面一直处于等待状态
【错误信息】:
com.alibaba.druid.pool.DruidDataSource-create connection SQLException, url:xxx, errorCode 0, state 08S01
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
【原始代码】:
public static void getDataSource(DataConfig dataConfig) throws Exception{
try {
Properties properties = new Properties();
properties.setProperty("driverClassName",dataConfig.getDriverClassName());
properties