[英]Failed to load driver class org.mariadb.jdbc.Driver
I want to configure 2 JNDI datasources in Spring Boot.我想在 Spring Boot 中配置 2 个 JNDI 数据源。 I tried this configuration:
我试过这个配置:
application.properties应用程序.properties
spring.production-datasource.jndi-name=java:/global/production_gateway
spring.production-datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.warehouse-datasource.jndi-name=java:/global/production_warehouse
spring.warehouse-datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
Primary datasource configuration:主数据源配置:
@Configuration
@EnableJpaRepositories(
basePackages = "org.datalis.plugin.production.entity",
entityManagerFactoryRef = "productionEntityManager",
transactionManagerRef = "productionTransactionManager"
)
@EnableTransactionManagement
public class ContextProductionDatasource {
@Autowired
private Environment env;
@Primary
@Bean(name = "productionDataSourceProperties")
@ConfigurationProperties(prefix="spring.production.datasource")
public DataSourceProperties productionDataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean(name = "productionDataSource")
@ConfigurationProperties(prefix="spring.production.datasource")
public DataSource productionDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "productionEntityManager")
public EntityManager productionEntityManager(EntityManagerFactory emf) {
return emf.createEntityManager();
}
@Primary
@Bean(name = "productionTransactionManager")
public PlatformTransactionManager productionTransactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Primary
@Bean(name = "productionExceptionTranslation")
public PersistenceExceptionTranslationPostProcessor productionExceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
Second datasource configuration:第二个数据源配置:
@Configuration
@EnableJpaRepositories(
basePackages = "org.datalis.plugin.warehouse.entity",
entityManagerFactoryRef = "warehouseEntityManager",
transactionManagerRef = "warehouseTransactionManager"
)
@EnableTransactionManagement
public class ContextWarehouseDatasource {
@Autowired
private Environment env;
@Bean(name = "warehouseDataSourceProperties")
@ConfigurationProperties(prefix="spring.warehouse.datasource")
public DataSourceProperties warehouseDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "warehouseDataSource")
@ConfigurationProperties(prefix="spring.warehouse.datasource")
public DataSource warehouseDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "warehouseEntityManager")
public EntityManager warehouseEntityManager(EntityManagerFactory emf) {
return emf.createEntityManager();
}
@Bean(name = "warehouseTransactionManager")
public PlatformTransactionManager warehouseTransactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean(name = "warehouseExceptionTranslation")
public PersistenceExceptionTranslationPostProcessor warehouseExceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
But I get error during deployment:但是在部署过程中出现错误:
Caused by: java.lang.IllegalStateException: Unable to set value for property driver-class-name
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.JavaBeanBinder$BeanProperty.setValue(JavaBeanBinder.java:349)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:96)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:79)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:56)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.Binder.lambda$bindDataObject$5(Binder.java:452)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.Binder$Context.withIncreasedDepth(Binder.java:570)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.Binder$Context.withDataObject(Binder.java:556)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.Binder$Context.access$400(Binder.java:513)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.Binder.bindDataObject(Binder.java:450)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:391)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:320)
... 132 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at deployment.datalis_rest_api.war//org.springframework.boot.context.properties.bind.JavaBeanBinder$BeanProperty.setValue(JavaBeanBinder.java:346)
... 142 more
Caused by: java.lang.RuntimeException: Failed to load driver class org.mariadb.jdbc.Driver in either of HikariConfig class loader or Thread context classloader
at deployment.datalis_rest_api.war//com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:485)
I'm using Spring boot war deployed into Wildfly server with deployed mariadb-java-client-2.4.2.jar.我正在使用部署到 Wildfly 服务器的 Spring boot war 和已部署的 mariadb-java-client-2.4.2.jar。 It's working fine when I use single datasource configuration.
当我使用单一数据源配置时它工作正常。 But with second datasource I get exception.
但是对于第二个数据源,我得到了异常。
Do you know how I can fix this issue?你知道我该如何解决这个问题吗?
Second attempt :第二次尝试:
application.properties:应用程序.properties:
spring.production.datasource.jndi-name=java:/global/production_gateway
spring.production.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.production.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.production.jpa.show-sql = true
spring.production.jpa.hibernate.ddl-auto = update
spring.warehouse.datasource.jndi-name=java:/global/production_warehouse
spring.warehouse.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.warehouse.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.warehouse.jpa.show-sql = true
spring.warehouse.jpa.hibernate.ddl-auto = update
First datasource:第一个数据源:
@Configuration
@EnableJpaRepositories(
basePackages = "org.datalis.plugin.production.entity",
entityManagerFactoryRef = "productionEntityManager",
transactionManagerRef = "productionTransactionManager"
)
@EnableTransactionManagement
public class ContextProductionDatasource {
@Primary
@Bean(name = "productionDataSourceProperties")
@ConfigurationProperties(prefix="spring.production")
public DataSourceProperties productionDataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean(name = "productionDataSource")
@ConfigurationProperties(prefix="spring.production")
public DataSource productionDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "productionEntityManager")
public EntityManager productionEntityManager(EntityManagerFactory emf) {
return emf.createEntityManager();
}
@Primary
@Bean(name = "productionTransactionManager")
public PlatformTransactionManager productionTransactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Primary
@Bean(name = "productionExceptionTranslation")
public PersistenceExceptionTranslationPostProcessor productionExceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
Second datasource:第二个数据源:
@Configuration
@EnableJpaRepositories(
basePackages = "org.datalis.plugin.warehouse.entity",
entityManagerFactoryRef = "warehouseEntityManager",
transactionManagerRef = "warehouseTransactionManager"
)
@EnableTransactionManagement
public class ContextWarehouseDatasource {
@Bean(name = "warehouseDataSourceProperties")
@ConfigurationProperties(prefix="spring.warehouse")
public DataSourceProperties warehouseDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "warehouseDataSource")
@ConfigurationProperties(prefix="spring.warehouse")
public DataSource warehouseDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "warehouseEntityManager")
public EntityManager warehouseEntityManager(EntityManagerFactory emf) {
return emf.createEntityManager();
}
@Bean(name = "warehouseTransactionManager")
public PlatformTransactionManager warehouseTransactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean(name = "warehouseExceptionTranslation")
public PersistenceExceptionTranslationPostProcessor warehouseExceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
Now I get:现在我得到:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at deployment.datalis_rest_api.war//org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at deployment.datalis_rest_api.war//org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:640)
... 41 more
Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
correct the name of both entityManager beans: productionEntityManager and warehouseEntityManager更正两个 entityManager bean 的名称:productionEntityManager 和 warehouseEntityManager
entityManagerFactoryRef
in @EnableJpaRepositories
is the beanName reference, @EnableJpaRepositories
中的entityManagerFactoryRef
是 beanName 引用,
so use所以用
@Configuration
@EnableJpaRepositories(
basePackages = "org.datalis.plugin.production.entity",
entityManagerFactoryRef = "productionEntityManager",
transactionManagerRef = "productionTransactionManager"
)
@EnableTransactionManagement
public class ContextProductionDatasource {
...
@Primary
@Bean
public EntityManager productionEntityManager(EntityManagerFactory emf) {
return emf.createEntityManager();
}
and和
@Configuration
@EnableJpaRepositories(
basePackages = "org.datalis.plugin.warehouse.entity",
entityManagerFactoryRef = "warehouseEntityManager",
transactionManagerRef = "warehouseTransactionManager"
)
@EnableTransactionManagement
public class ContextWarehouseDatasource {
...
@Bean
public EntityManager warehouseEntityManager(EntityManagerFactory emf) {
return emf.createEntityManager();
}
you need to match the property-name in @ConfigurationProperties
to the value of properties ' - ' versus ' .您需要将
@ConfigurationProperties
中的属性名称与属性 ' - ' 与 ' 的值相匹配。 ' '
use @ConfigurationProperties(prefix="spring.production-datasource")
and @ConfigurationProperties(prefix="spring.warehouse-datasource")
使用
@ConfigurationProperties(prefix="spring.production-datasource")
和@ConfigurationProperties(prefix="spring.warehouse-datasource")
and make sure you have have driver in your classpath并确保您的类路径中有驱动程序
like喜欢
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.5.2</version>
<scope>runtime</scope>
</dependency>
In your properties you are using spring.production-datasource
however in the Spring @Configuration
you are attempting to map it with @ConfigurationProperties(prefix = "spring.production.datasource")
.在您的属性中,您正在使用
spring.production-datasource
但是在 Spring @Configuration
您正在尝试使用 map 使用@ConfigurationProperties(prefix = "spring.production.datasource")
. Starting from Spring Boot 2 the relaxed property binding rules are now more strict so there is a mismatch between a dash and a dot here.从 Spring Boot 2 开始,宽松的属性绑定规则现在更加严格,因此这里的破折号和点之间存在不匹配。
You either need to change properties to spring.production.datasource
or use @ConfigurationProperties(prefix = "spring.production-datasource")
.您需要将属性更改为
spring.production.datasource
或使用@ConfigurationProperties(prefix = "spring.production-datasource")
。
When configuring Spring Boot DataSource
you can either use jndi-name
or provide the connection details, not both.配置 Spring 引导
DataSource
时,您可以使用jndi-name
或提供连接详细信息,不能同时使用两者。 As per Common Application properties :根据通用应用程序属性:
spring.datasource.jndi-name
spring.datasource.jndi-name
JNDI location of the datasource.
数据源的 JNDI 位置。 Class, url, username & password are ignored
Class,url,用户名和密码被忽略
You most likely have to remove spring.production-datasource.driver-class-name
property as it collides with jndi-name
.您很可能必须删除
spring.production-datasource.driver-class-name
属性,因为它与jndi-name
冲突。
(Disclaimer: I'm complete newbie to Spring Boot, H2, Maven, etc.) (免责声明:我是 Spring Boot、H2、Maven 等的新手)
Just my 2c in case some other newbie lands on this question page after having gotten this particular error:只是我的 2c,以防其他新手在收到此特定错误后登陆此问题页面:
I got this error immediately after having added the dependency to pom.xml
and needed properties into the application.properties
file.在将依赖项添加到
pom.xml
并将需要的属性添加到application.properties
文件后,我立即收到此错误。
Trying to run the application from Eclipse IDE would just fail with this Failed to load driver class org.mariadb.jdbc.Driver
error.尝试从 Eclipse IDE 运行应用程序会失败,并显示此
Failed to load driver class org.mariadb.jdbc.Driver
错误。 My first thought was that maybe Maven didn't pick up the dependency for some reason, but the mariadb-java-client.jar
file was in ~/.m2/repository/org/mariadb/jdbc
, thus I assumed everything was correct there.我的第一个想法是 Maven 可能由于某种原因没有获取依赖项,但是
mariadb-java-client.jar
文件在~/.m2/repository/org/mariadb/jdbc
中,因此我假设那里的一切都是正确的.
Quick googling showed there should not be much else to do, it just should work(tm).快速谷歌搜索表明应该没有太多其他事情要做,它应该可以工作(tm)。 I was out of ideas and semi-randomly invoked
mvnw clean install
from shell, and everything worked just fine there - the application started as it should.我没有想法,半随机地从 shell 调用
mvnw clean install
,一切正常——应用程序正常启动。
Empowered by this massive random success, I closed & reopened the project in Eclipse. Lo and behold;受到这种巨大随机成功的鼓舞,我在 Eclipse 中关闭并重新打开了该项目。瞧瞧; magically everything started working there as well.
神奇的是,一切也开始在那里工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.