Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Not loading a JDBC driver as driverClassName property is null, in springboot working with two datasources

Tags:

spring-boot

I have currently configured spring boot to work with two different datasources. The application is working fine, however when I start the spring boot application I get an warning repeated 10 times like below:

2018-06-05 10:28:15.897 WARN 8496 --- [r://myScheduler] o.a.tomcat.jdbc.pool.PooledConnection : Not loading a JDBC driver as driverClassName property is null.

As I mentioned this is not affecting my application, but I would like to know why I am having this kind of warning and if there is any way to fix it.

like image 334
Joan Plepi Avatar asked Jun 05 '18 08:06

Joan Plepi


1 Answers

When using two or more datasources you need to configure them yourself. In that case Spring Boot's DataSourceAutoConfiguration (and also DataSourceProperties) won't be used.

You most probably have the related DB details like the name of the JDBC driver class name in application.properties file as follows:

spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

primary.datasource.url=jdbc:sqlserver://xxx.yyy.zzz.www:1433;databaseName=primaryDB
primary.datasource.username=username
primary.datasource.password=password

other.datasource.url=jdbc:sqlserver://xxx.yyy.zzz.www:1433;databaseName=otherDb
other.datasource.username=otheruser
other.datasource.password=otherpassword

Thus, to set the driver class name for the datasource just say:

@Value("${spring.datasource.driver-class-name}")
String driverClassName;

@Primary
@Bean(name = "primaryDb")
@ConfigurationProperties(prefix = "primary.datasource")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().driverClassName(driverClassName).build();
}

@Bean(name = "otherDb")
@ConfigurationProperties(prefix = "other.datasource")
public DataSource otherDataSource() {
    return  DataSourceBuilder.create().driverClassName(driverClassName).build();
}
like image 198
skataja Avatar answered Nov 15 '22 08:11

skataja