需要加个验证:
spring.datasource.druid.validation-query=select 'x'
项目中是动态的数据源,因此代码中更改一下:
DruidDataSource dataSource = new DruidDataSource(); String driverClassName = dbSource.getDbDriver(); String url = dbSource.getDbUrl(); String dbUser = dbSource.getDbUsername(); String dbPassword = dbSource.getDbPassword(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); //dataSource.setValidationQuery("SELECT 1 FROM DUAL"); if(driverClassName.equals("com.microsoft.sqlserver.jdbc.SQLServerDriver")) { //sqlserver数据库要加这个 dataSource.setValidationQuery("select 'x'"); } dataSource.setTestWhileIdle(true); dataSource.setTestOnBorrow(false); dataSource.setTestOnReturn(false); dataSource.setBreakAfterAcquireFailure(true); dataSource.setConnectionErrorRetryAttempts(0); dataSource.setUsername(dbUser); dataSource.setMaxWait(60000); dataSource.setPassword(dbPassword);
根据driverClassName来判断是否sqlserver,然后加个验证
参考:https://github.com/alibaba/druid/issues/4277
github很难访问,百度出来修改host文件的方法没有用处,我的办法是不停刷新,迟早是能出来的~