Reputation: 389
I'm trying to configure two datasources in my spring boot app but by doing that I'm not sure if the Autoconfiguration
option of spring should be turn off or if I'm missing some code.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pe.com.test</groupId>
<artifactId>Dashboard</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
# Oracle settings
#DB1
db1.datasource.url=jdbc:oracle:thin:@192.168.1.2:1521:DB1
db1.datasource.username=test
db1.datasource.password=abc123
db1.datasource.driver.class=oracle.jdbc.driver.OracleDriver
#DB2
db2.datasource.url=jdbc:oracle:thin:@192.168.1.3:1521:DB2
db2.datasource.username=test
db2.datasource.password=abc123
db2.datasource.driver.class=oracle.jdbc.driver.OracleDriver
My config class.
@Configuration
public class DataSourceConfiguration {
@Bean
@Primary
@ConfigurationProperties("db1.datasource")
public DataSource db1Ds() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("db2.datasource")
public DataSource db2Ds() {
return DataSourceBuilder.create().build();
}
@Bean
@Autowired
@Primary
DataSourceTransactionManager db1Tm(@Qualifier("db1Ds") DataSource dataSource) {
DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource);
return dstm;
}
@Bean
@Autowired
DataSourceTransactionManager db2Tm(@Qualifier("db2Ds") DataSource dataSource) {
DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource);
return dstm;
}
}
I know that since I'm changing the default properties for autoconfiguring the database spring.datasource
when starting the app It should give an error.
2020-04-04 16:02:25.654 WARN 15076 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-04-04 16:02:25.665 INFO 15076 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-04 16:02:25.667 ERROR 15076 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
I can bypass these error just by turning off spring autoconfiguration.
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
What I want to know if I'm on the right path by turning DataSourceAutoConfiguration
off or if I'm missing some other configuration in order to make autoconfigure work.
Upvotes: 0
Views: 480
Reputation: 519
Better try with these database properties and configurations
#DB1
spring.db1-datasource.url=jdbc:oracle:thin:@192.168.1.2:1521:DB1
spring.db1-datasource.username=test
spring.db1-datasource.password=abc123
spring.db1-datasource.driver.class=oracle.jdbc.driver.OracleDriver
#DB2
spring.db2-datasource.url=jdbc:oracle:thin:@192.168.1.3:1521:DB2
spring.db2-datasource.username=test
spring.db2-datasource.password=abc123
spring.db2-datasource.driver.class=oracle.jdbc.driver.OracleDriver
@Configuration
public class DataSourceConfiguration {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.db1-datasource")
public DataSource db1Ds() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.db2-datasource")
public DataSource db2Ds() {
return DataSourceBuilder.create().build();
}
@Bean
@Autowired
@Primary
DataSourceTransactionManager db1Tm(@Qualifier("db1Ds") DataSource dataSource) {
DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource);
return dstm;
}
@Bean
@Autowired
DataSourceTransactionManager db2Tm(@Qualifier("db2Ds") DataSource dataSource) {
DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource);
return dstm;
}
}
Upvotes: 1