Debug - HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required 解决方案

回到顶部(go to top)

问题表现

我自己遇到的问题是:

### Error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
### The error may exist in class path resource [mapper/risk-curve-mapper.xml]
### The error may involve com.huatai.quant.database.dao.RiskCurveDao.findLastQuantCurveData
### The error occurred while executing a query
### Cause: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
### The error may exist in class path resource [mapper/risk-curve-mapper.xml]
### The error may involve com.huatai.quant.database.dao.RiskCurveDao.findLastQuantCurveData
### The error occurred while executing a query
### Cause: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.

......

Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:786)
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:92)
at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:159)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:117)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:80)
at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67)
at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:337)
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
... 28 more

 

回到顶部(go to top)

问题详细解析

必看:HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.解决方案

 

回到顶部(go to top)

解决方案

case 1:注入属性要修改

 

 

 

case 2:自己构建HikariDataSource(当DataSource是自己创建的bean)

必看:HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.解决方案

 

 

case3:重新注入缺失的属性(当DataSource是第三方包提供的bean)

如果第三方工具提供的bean,没有用到@Primary注解,尚且还能把自己的bean加上@Primary注解来替换它。

一旦第三方工具提供的bean,用到了@Primary注解,那就只能从applicationContext里把它拿出来,手动注入一次。

 

step1: 用ApplicationContextAware接口,获取applicationContext

SpringBoot中获取Bean的三种方式: https://blog.csdn.net/DDDYSz/article/details/123525479

复制代码
@Component
public class ScenarioOracleServiceImpl implements ScenarioBaseService, ApplicationContextAware {


//.....


@Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

  /**
     * 从applicationContext拿出已注入的DataSource
     *
     * @param clazz
     * @return
     */
    public DataSource getDataSourceBean(Class<DataSource> clazz) {
        return this.applicationContext != null ? this.applicationContext.getBean(clazz) : null;
    }

}
复制代码

 

 

 

 

step2:手动注入需要的属性

复制代码
/**
     * 因为从fane-data-sdk注入的HikariDataSource,always为空,各种属性没有注入。
     * 因此这里人工注入
     */
    public void validateExistingDataSource() {
        DataSource dataSourceBean = getDataSourceBean(DataSource.class);
        if (dataSourceBean instanceof HikariDataSource) {

            HikariConfig hikariConfig = new HikariConfig();
            hikariConfig.setJdbcUrl(jdbcUrl);
            hikariConfig.setUsername(userName);
            hikariConfig.setPassword(password);
            hikariConfig.setDriverClassName(driverClassName);
            HikariDataSource mySource = new HikariDataSource(hikariConfig);

            HikariDataSource hikariDataSource = (HikariDataSource) dataSourceBean;
            hikariDataSource.setDataSource(mySource);
        }
    }
复制代码

 

posted on   frank_cui  阅读(3137)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-06-20 Nginx - Nginx常用命令
2021-06-20 Nginx - 正向代理 vs 反向代理
2021-06-20 Nginx - Nginx三大功能
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

levels of contents
点击右上角即可分享
微信分享提示