Search code examples
javaspringhibernatemysql-connectorhikaricp

HikariCP + Hibernate + MySql: warnings even when defining correct 'driverClassName' property


I'm having this problem with HikariCP and MySql in my maven project: a warning shows up saying:

Loading class 'com.mysql.jdbc.Driver'. This is deprecated. The new driver class is 'com.mysql.cj.jdbc.Driver'. The driver has automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

I want to get rid of this warning.

For that, I need to know why even having the maven MySql connector in its latest version and HikariCP configuration set to set the class com.mysql.cj.jdbc.Driver (which is the non-deprecated one) this warning still shows up.

Here it is the maven dependency in pom.xml:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>

Then, I have this properties file called hikaricp-hibernate.properties

jdbcUrl=jdbc:mysql://localhost:3306/local_database
driverClassName=com.mysql.cj.jdbc.Driver

#user and password and other data omitted

Now, for the HibernateConfiguration I have:

package mypackage;

// ... imports

@Configuration
@EnableTransactionManagement

public class HibernateConfig {
    private static final String HIBERNATE_PROPERTIES = "/hikaricp-hibernate.properties";

    @Bean(name = "hikariDataSource")
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig(HIBERNATE_PROPERTIES);
        HikariDataSource dataSource = new HikariDataSource(config);
        return dataSource;
    }
}

In the same class, I register as beans the LocalSessionFactoryBean and the TransactionManager, but the code does not matter here.

I also already checked the configuration in debug mode to see if the driverClassName is the one I have put and the answer is yes.

Therefore, the logs are shown even if registered correctly.

Also, HikariCP docs tells me to use the jdbcUrl configuration:

The MySQL DataSource is known to be broken with respect to network timeout support. Use jdbcUrl configuration instead.

Why?

Does jdbcUrl configuration triggers automatically the old Class (if yes, how can I override and avoid this?)? And then it seems that it is deprecated and searches for another one? Does it ignore my driverClassName config?


Solution

  • There's no issue with the code that's here - the key points being:

    • You're listing a dependency on the 8.* branch of the mysql connector, which contains the new JDBC driver
    • You're listing the new driver under driverClassName in your properties file
    • You're instantiating the HikariConfig correctly, with the properties that you're defining.

    If the old driver is being loaded, it therefore isn't in this code - it's either with a separate application, or somewhere else in this application.