您现在的位置是:网站首页 > 代码编程 > JAVA开发JAVA开发

【原】springboot配置双数据源报错“jdbcUrl is required with driverClassName”

不忘初心 不忘初心 2020-01-14 围观() 评论() 点赞() JAVA开发

简介:最近的springboot项目中涉及到了双数据源的配置,照着网上的教程配置时,出了一点儿小问题,启动的直接报错“Caused by: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.”。

最近的springboot项目中涉及到了双数据源的配置,照着网上的教程配置时,出了一点儿小问题,启动的直接报错“Caused by: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.”。

详细错误信息如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 
2019-11-22 17:55:40,839 default [main] ERROR o.s.b.SpringApplication 4444 - Application run failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactorySecondary' defined in class path resource [com/zhiri/datacenter/task/conf/SecondaryConfig.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: secondaryPersistenceUnit] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1803)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
	at com.zhiri.datacenter.task.application.DataCenterApplication.main(DataCenterApplication.java:22)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: secondaryPersistenceUnit] Unable to build Hibernate SessionFactory; nested exception is java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:403)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1862)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1799)
	... 15 common frames omitted
Caused by: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
	at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:954)
	at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:109)
	at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180)
	at org.hibernate.hql.spi.id.IdTableHelper.executeIdTableCreationStatements(IdTableHelper.java:67)
	at org.hibernate.hql.spi.id.global.GlobalTemporaryTableBulkIdStrategy.finishPreparation(GlobalTemporaryTableBulkIdStrategy.java:125)
	at org.hibernate.hql.spi.id.global.GlobalTemporaryTableBulkIdStrategy.finishPreparation(GlobalTemporaryTableBulkIdStrategy.java:42)
	at org.hibernate.hql.spi.id.AbstractMultiTableBulkIdStrategyImpl.prepare(AbstractMultiTableBulkIdStrategyImpl.java:88)
	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:313)
	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1249)
	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391)
	... 19 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:55684', transport: 'socket'

jdbc配置如下:

#写库
spring.primary.datasource.url=jdbc:mysql://xxx
spring.primary.datasource.username=xxx
spring.primary.datasource.password=xxx
spring.primary.datasource.driver-class-name=com.mysql.jdbc.Driver
#读库
#spring.secondary.datasource.url=jdbc:oracle:thin:@xxx
#spring.secondary.datasource.username=xxx
#spring.secondary.datasource.password=xxx
#spring.secondary.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

最后发现确实是这个jdbc的配置有问题,在只有一个数据源的时候,直接使用的是spring.datasource.url=xxx,这个是没有问题的,但是在双数据源的时候,就不能再这么写了。

官方文档的解释:

因为连接池的实际类型没有被公开,所以在您的自定义数据源的元数据中没有生成密钥,而且在IDE中没有完成(因为DataSource接口没有暴露属性)。另外,如果您碰巧在类路径上有Hikari,那么这个基本设置就不起作用了,因为Hikari没有url属性(但是确实有一个jdbcUrl属性)。在这种情况下,您必须重写您的配置。

改进后的写法:

#写库
spring.primary.datasource.jdbc-url=jdbc:mysql://xxx
spring.primary.datasource.username=xxx
spring.primary.datasource.password=xxx
spring.primary.datasource.driver-class-name=com.mysql.jdbc.Driver
#读库
#spring.secondary.datasource.jdbc-url=jdbc:oracle:thin:@xxx
#spring.secondary.datasource.username=xxx
#spring.secondary.datasource.password=xxx
#spring.secondary.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

springboot

看完文章,有任何疑问,请加入群聊一起交流!!!

很赞哦! ()

文章评论

  • 请先说点什么
    人参与,条评论

请使用电脑浏览器访问本页面,使用手机浏览器访问本页面会导致下载文件异常!!!

雨落无影

关注上方公众号,回复关键字【下载】获取下载码

用完即删,每次下载需重新获取下载码

若出现下载不了的情况,请及时联系站长进行解决

站点信息

  • 网站程序:spring + freemarker
  • 主题模板:《今夕何夕》
  • 文章统计:篇文章
  • 标签管理标签云
  • 微信公众号:扫描二维码,关注我们