Error:Failed to load driver class com.microsoft.sqlserver.jdbc.SQLServerDriver from HikariConfig class classloader

Error:Failed to load driver class com.microsoft.sqlserver.jdbc.SQLServerDriver from HikariConfig class classloader

The error indicates a failure to load the SQL Server JDBC driver (com.microsoft.sqlserver.jdbc.SQLServerDriver) in the context of HikariCP (connection pool) during the initialization of a Spring Boot application. This failure is causing a subsequent issue with bean initialization related to a data source. Here are steps to troubleshoot and resolve this issue:
1. Check JDBC Driver Dependency: Ensure that the JDBC driver for Microsoft SQL Server is included in your project dependencies. You need to have the appropriate JAR file for the SQL Server JDBC driver. For Maven, you can add the following dependency to your pom.xml:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>your_driver_version</version>
</dependency>

2. Verify Classpath: Confirm that the SQL Server JDBC driver JAR is in your application’s classpath. Check your project configuration and dependencies to ensure that the driver is available at runtime.
3.Check HikariCP Configuration: Review your HikariCP configuration to ensure that you have specified the correct JDBC URL, username, password, and driver class. Make sure that the driver class name matches the one for SQL Server. Example HikariCP configuration in application.properties or application.yml:

spring.datasource.url=jdbc:sqlserver://your_server:1433;databaseName=your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

4.Check Spring Data Source Configuration: Examine your Spring Boot application’s data source configuration. Ensure that your DataSource bean is correctly configured. Check the relevant configuration in your application.

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "barEntityManagerFactory",
                       transactionManagerRef = "barTransactionManager", basePackages = {"com.spicejet.rosteringLogbook.arms.repo"})
public class SqlDBConfig {

    @Bean(name = "barDataSource")
    @ConfigurationProperties(prefix = "db_name.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "barEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
            EntityManagerFactoryBuilder builder, @Qualifier("barDataSource") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("name_of_the_package").persistenceUnit("db_name")
                .build();
    }

    @Bean(name = "barTransactionManager")
    public PlatformTransactionManager barTransactionManager(
            @Qualifier("barEntityManagerFactory") EntityManagerFactory barEntityManagerFactory) {
        return new JpaTransactionManager(barEntityManagerFactory);
    }

}

5. Check Database Initialization Configuration: If you are using database initialization scripts, ensure that they are correctly configured. The error log mentions a problem with DataSourceInitializationConfiguration and the bean dataSourceScriptDatabaseInitializer.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *