Using two datasources with different jdbc drivers with Spring Boot

1 answer

I followed the documentation and created some sourcecode that is quite identical to the given example. When I run my code I get this exception:

java.lang.RuntimeException: Driver org.mariadb.jdbc.Driver claims to not accept jdbcUrl, jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

The strange thing is that two things are mixed:

  • the h2-jdbcUrl is treated by the mariaDB-driver
  • the query that my previously existing code runs it targetted against mariaDB (I have not yet written any code that used the second datasource)

These are my beans:

@Bean @Primary @ConfigurationProperties("datasources.first") public DataSourceProperties firstDataSourceProperties() {     return new DataSourceProperties(); }  @Bean @Primary @ConfigurationProperties("datasources.first.configuration") public HikariDataSource firstDataSource() {     return firstDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build(); }  @Bean @ConfigurationProperties("datasources.second") public DataSourceProperties secondDataSourceProperties() {     return new DataSourceProperties(); }  @Bean @ConfigurationProperties("datasources.second.configuration") public HikariDataSource secondDataSource() {     return secondDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build(); } 

My application.yml looks like this:

datasources:   first:     jdbcUrl: jdbc:mariadb://...     username: ...     password: ...     driver-class-name: org.mariadb.jdbc.Driver   second:     jdbcUrl: jdbc:h2:/tmp/test;DB_CLOSE_ON_EXIT=FALSE     username: ...     password: ...     driver-class-name: org.h2.Driver 

This is the code that causes the exception:

@Component public class Updater {      @Autowired     JdbcTemplate jdbcTemplate;      @Autowired     Queries queries;      public List<User> getActiveUsers() throws Exception {         List<User> users = jdbcTemplate.query(queries.getActive(), new UserRowMapper());          return users;     } 

In order to diagnose this further I enabled the configprops and beans endpoints and checked their output. The config looks good and the beans too:

Config (one of many):

    "firstDataSourceProperties": {       "prefix": "datasources.first",       "properties": {         "password": "******",         "initializationMode": "EMBEDDED",         "driverClassName": "org.mariadb.jdbc.Driver",         "generateUniqueName": false,         "xa": {           "properties": {}         },         "separator": ";",         "platform": "all",         "continueOnError": false,         "username": "..."       } 

Bean (one of many):

"firstDataSource": {           "aliases": [],           "scope": "singleton",           "type": "com.zaxxer.hikari.HikariDataSource",           "resource": "...Application",           "dependencies": [             "firstDataSourceProperties"           ]         } 

I use Spring Boot 2.2.6

All answers to this question, which has the identifier 61228431

The best answer:

This problem was caused by me not understanding the interaction between DataSourceProperties and HikariDataSource.

I found out that the jdbcUrl didn't carry the value I configured, this could be seen by the details of the firstDataSource:

"firstDataSource": {   "prefix": "datasources.first.configuration",   "properties": {     "initializationFailTimeout": 1,     "validationTimeout": 5000,     "readOnly": false,     "registerMbeans": false,     "healthCheckProperties": {},     "isolateInternalQueries": false,     "leakDetectionThreshold": 0,     "maxLifetime": 1800000,     "minimumIdle": 10,     "metricsTrackerFactory": {},     "allowPoolSuspension": false,     "idleTimeout": 600000,     "dataSourceProperties": {},     "driverClassName": "org.mariadb.jdbc.Driver",     "jdbcUrl": "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE",     "loginTimeout": 0,     "maximumPoolSize": 10,     "autoCommit": true,     "connectionTimeout": 30000,     "username": "...",     "poolName": "HikariPool-1" } 

As you can see the jdbcUrl addresses h2. This is caused by the fact that I set "my" intended url at datasources.first.jdbcUrl. But after that the (non-existing) value of datasources.first.configuration.jdbcUrl was read which was not configured and obviously set to a default.

After I changed my application.yml to this it worked:

datasources:   first:     username: ...     password: ...     driver-class-name: org.mariadb.jdbc.Driver     configuration:       jdbcUrl: jdbc:mariadb://... 

I will need to decide which value to put where, the username and password could be moved one level deeper too, but that's another story ...

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data