其他分享
首页 > 其他分享> > Debug - HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required 解决方案

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

作者:互联网

问题表现

我自己遇到的问题是:

### 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

 

问题详细解析

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

 

解决方案

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);
        }
    }

 

标签:java,required,HikariPool,dataSourceClassName,ibatis,HikariDataSource,apache,org,
来源: https://www.cnblogs.com/frankcui/p/16392536.html