我想在Spring Boot中配置2个JNDI数据源。我尝试了这个配置:
application.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
主要数据源配置:
@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();
}
}
第二个数据源配置:
@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();
}
}
但是我在部署过程中收到错误:
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)
我正在使用部署到Wildfly服务器的Spring Boot战mariadb-java-client-2.4.2.jar.当我使用单个数据源配置时,它工作正常。但是对于第二个数据源,我得到了异常。
你知道我如何解决这个问题吗?
第二次尝试:
application.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
第一个数据源:
@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();
}
}
第二个数据源:
@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();
}
}
现在我得到:
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.
在属性中,您使用的是spring。生产数据源但是,在Spring配置中,您试图将其映射为配置属性(前缀=“Spring.production.datasource”)。从Spring Boot 2开始,松弛的属性绑定规则现在更加严格,因此这里的破折号和点不匹配。
您需要将属性更改为spring.production.datasource
或使用@ConfigurationProperties(prefix="spring.production-datassource")
。
配置Spring BootDataSource
时,您可以使用jndi-name
或提供连接详细信息,而不是两者兼而有之。根据通用应用程序属性:
Spring数据源。jndi名称
数据源的JNDI位置。类、url、用户名
您很可能需要删除spring。生产数据源。驱动程序类名与jndi名称冲突时的属性。
更正entityManager bean的名称:productionEntityManager和warehouseEntityManager
EnableJpaRepositories中的entityManagerFactoryRef是beanName参考,
所以使用
@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();
}
和
@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();
}
您需要将@ConfigurationProperties
中的属性名称与属性'-'对'的值匹配。
使用@ConfigurationProperties(prefix="spring.production-数据源")
和@ConfigurationProperties(prefix="spring.warehouse-数据源")
确保类路径中有驱动程序
喜欢
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.5.2</version>
<scope>runtime</scope>
</dependency>
我的Spring Boot项目正在尝试使用驱动程序连接MYSQL数据库。我已经导入了最新的mysql驱动程序和 我已经在文件中配置了数据库连接 MYSQL版本是8.0.26 Spring启动版本2.6.2 当用Intellij运行项目时,我得到一个错误 原因:org。springframework。豆。BeanInstationException:未能实例化[com.zaxxer.hikari.H
我正在运行以下代码,但是我得到了关于Oracle类名称的错误。我已经用oracle jar文件设置了classpath环境变量,但它不起作用。有人能帮我吗?我不知道还能做什么。我真的很感谢你的帮助 这就是错误:由:java引起。sql。SQLException:无法加载JDBC驱动程序类“oracle”。jdbc。驾驶员OracleDriver's
问题内容: 我在春季配置了一个netezza db。我在类路径中添加了依赖的nzjdbc.jar 春季配置: 问题答案: 将放入您的本地Maven存储库 (在该目录执行这个位于 然后像正常依赖项一样使用它: @请参阅安装第三方JAR的指南
即使驱动程序类名已定义,我也会收到此错误 Java语言lang.IllegalStateException:无法加载驱动程序类:com。mysql。jdbc。组织的驱动程序。springframework。util。明确肯定state(Assert.java:392)~[spring-core-4.2.1.RELEASE.jar:4.2.1.RELEASE]位于org。springframewor
无法连接。无法建立到jdbc的连接:derby://localhost:1527/sample使用组织。阿帕奇。德比。jdbc。ClientDriver(DERBY SQL错误:错误代码:40000,SQLSTATE:XJ040,SQLERRMC:无法使用类加载器sun.misc.Launcher启动数据库'sample'$AppClassLoader@1d44bcfa,有关详细信息,请参见下一个
我正在上使用。我试图弄明白如何使用数据导入从加载数据。但我最终无法加载JDBC驱动程序类。以下是我所做的: 把放到 1.配置: 2.配置: 在