博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required)
阅读量:7231 次
发布时间:2019-06-29

本文共 3764 字,大约阅读时间需要 12 分钟。

最近在项目中想试一下使用 Hikari 连接池,以前用的是阿里的 Druid,框架是 Spring MVC,xml配置文件方式注入的 Bean,现在换成 Spring Boot 之后,总遇到一些奇怪的问题,问题的根源是在于自己是个半桶水。

好了,先来看看 application.yml 配置文件:

spring:  jpa:    show-sql: true  datasource:    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true    username: root    password: root    type: com.zaxxer.hikari.HikariDataSource    hikari:      maximum-pool-size: 20      max-lifetime: 30000      idle-timeout: 30000      data-source-properties:        prepStmtCacheSize: 250        prepStmtCacheSqlLimit: 2048        cachePrepStmts: true        useServerPrepStmts: true    slave:      url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true      username: root      password: root      type: com.zaxxer.hikari.HikariDataSource      hikari:        maximum-pool-size: 20        max-lifetime: 30000        idle-timeout: 30000        data-source-properties:          prepStmtCacheSize: 250          prepStmtCacheSqlLimit: 2048          cachePrepStmts: true          useServerPrepStmts: true复制代码

数据源配置文件:

package org.seckill.config;import com.zaxxer.hikari.HikariDataSource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.jdbc.core.JdbcTemplate;import javax.sql.DataSource;import java.util.HashMap;import java.util.Map;/** * 数据源配置 * * @author jeftom * @date 2019-04-14 12:03 * @since 1.0.0 */@Configurationpublic class DataSourceConfig {	private final static Logger LOGGER = LoggerFactory.getLogger(DataSourceConfig.class);	@Bean(name = "masterDataSource")	@ConfigurationProperties(prefix = "spring.datasource")	public DataSource masterDataSource(DataSourceProperties properties) {		LOGGER.info("init master data source:{}", properties);		return DataSourceBuilder.create().build();	}	@Bean(name = "slaveDataSource")	@ConfigurationProperties(prefix = "spring.datasource.slave")	public DataSource slaveDataSource(DataSourceProperties properties) {		LOGGER.info("init slave data source:{}", properties);		return DataSourceBuilder.create().build();	}	@Bean	@Primary	public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource) {		Map
targetDataSources = new HashMap<>(); targetDataSources.put(DataSourceEnum.MASTER.getName(), masterDataSource); targetDataSources.put(DataSourceEnum.SLAVE.getName(), slaveDataSource); return new DynamicDataSource(masterDataSource, targetDataSources); }}复制代码

报错信息:

com.zaxxer.hikari.HikariConfig           : HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.	at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:955) ~[HikariCP-3.2.0.jar:na]复制代码

百度了一下找到的解决方法:

  1. url 换成了 jdbc-url;
  2. 增加 driver-class-name: com.mysql.cj.jdbc.Driver

试了一下,果然真的可以。但是,没有使用多数据源时,原来的配置文件也是能正常使用的啊,为什么呢?

问题肯定是出在增加了 DataSourceConfig 这个配置文件之后。

于是试着把配置文件还原回去,再把

return DataSourceBuilder.create().build();复制代码

改为如下:

return DataSourceBuilder.create(properties.getClassLoader())				.type(HikariDataSource.class)				.driverClassName(properties.determineDriverClassName())				.url(properties.determineUrl())				.username(properties.determineUsername())				.password(properties.determinePassword())				.build();复制代码

也就是从配置文件拿到配置值之后重新赋值一下,再次启动项目,居然好了~

原因就是出在 DataSourceBuilder 创建数据源这个类上,而单数据源自动装载时不会出现这样的问题。

转载地址:http://rkjfm.baihongyu.com/

你可能感兴趣的文章
利用Ajax无刷新更新rss阅读
查看>>
GCC 64位程序的makefile条件编译心得——32位版与64位版、debug版与release版(兼容MinGW、TDM-GCC)...
查看>>
测试Varnish缓存服务器与IIS连接数
查看>>
POJ 2396 Budget
查看>>
编程之美:求二进制中1的个数
查看>>
Qt中父子widget的消息传递(转)
查看>>
vim 代码提示功能,让vim可以媲美IDE(转)
查看>>
IO笔记总结
查看>>
位棋盘表示法中车和炮的着法生成
查看>>
Arch Linux 安装 ibus 五笔输入法备忘录
查看>>
第38周星期二
查看>>
在VS2010里可以给JS函数添加代码提示\注释
查看>>
Speex手册----编解码介绍 中文翻译
查看>>
简单的手机号判断
查看>>
(五) solr 索引数据导入:csv格式
查看>>
Guava 1.5-Throwables:简化异常和错误的传播与检查
查看>>
开源 Java 模板引擎 HTTL 1.0.0 发布
查看>>
ASP.NET MVC4 音乐商店,Entity Framework 4.1 表名被自动转换成复数问题解决
查看>>
poj3308
查看>>
hdu1166(单点更新,区间求和)
查看>>