Spring Boot
database driver
embedded database
error resolution
troubleshooting

Spring Boot - Cannot determine embedded database driver class for database type NONE

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Spring Boot's ease of use and streamlined setup make it a popular choice for developing Java-based applications. Its ability to automatically configure applications based on the libraries present on the classpath is a significant advantage. However, this auto-configuration feature can sometimes lead to confusion, especially when dealing with database configurations. One particular error that developers encounter is: "Cannot determine embedded database driver class for database type NONE."

Understanding the Error

When Spring Boot attempts to configure a data source automatically, it expects a certain environment. The error "Cannot determine embedded database driver class for database type NONE" typically arises when Spring Boot cannot determine the database type and is therefore unable to configure a suitable data source.

Here are some of the scenarios when this error might occur:

  1. Missing Driver Dependency: Spring Boot cannot autodetect an embedded database if the necessary driver dependencies are missing from the classpath.
  2. Incorrect Database Configuration: No explicit configuration is provided in the application.properties or application.yml files, leading Spring Boot to search for an embedded database when it cannot find one.
  3. Unused Profile Activation: Profiles meant to switch data source configurations might not be activated correctly.

Spring Boot's AutoConfiguration

Spring Boot provides the spring-boot-starter-jdbc and spring-boot-starter-data-jpa starters, which are responsible for configuring a DataSource. By default, if these starters are on the classpath and an explicit DataSource is not configured, Spring Boot will attempt to detect an embedded database such as H2, HSQLDB, or Derby.

Spring Boot uses the following logic to determine the appropriate configuration:

  • It checks the classpath for embedded database drivers like H2, HSQLDB, or Derby.
  • If not found, the detection mechanism fails, and it defaults to reporting the "database type NONE."

Resolving the Error

Below are common steps to resolve this error:

1. Ensure the Correct Database Dependency

Ensure you have the necessary driver and embedded database dependencies. For example, for H2, include the following in your pom.xml:

xml
1<dependency>
2    <groupId>com.h2database</groupId>
3    <artifactId>h2</artifactId>
4    <scope>runtime</scope>
5</dependency>

2. Explicit Database Configuration

Override Spring Boot’s auto-configuration by explicitly providing the database configuration in application.properties or application.yml:

properties
1spring.datasource.url=jdbc:h2:mem:testdb
2spring.datasource.driver-class-name=org.h2.Driver
3spring.datasource.username=sa
4spring.datasource.password=password

3. Activate Spring Profile

If using profiles, ensure correct activation:

properties
spring.profiles.active=dev

Corresponding profile-specific properties should reside in application-dev.properties or similar.

4. Check for Application Context Issues

Ensure your application's context does not inadvertently exclude necessary configurations, which can happen due to misconfiguration of component scanning or improper context initialization.

Examples of Common Configurations

Here's a simple example of a project configured for H2 as the embedded DB:

pom.xml:

xml
1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-data-jpa</artifactId>
4</dependency>
5<dependency>
6    <groupId>com.h2database</groupId>
7    <artifactId>h2</artifactId>
8    <scope>runtime</scope>
9</dependency>

application.properties:

properties
1spring.datasource.url=jdbc:h2:mem:testdb
2spring.datasource.driver-class-name=org.h2.Driver
3spring.datasource.username=sa
4spring.datasource.password=password
5
6# Enable H2 console
7spring.h2.console.enabled=true
8spring.h2.console.path=/h2-console

Summary Table

Issue/AspectDescription/Action
Missing Driver DependencyEnsure driver dependencies for the intended database are included in your project.
Incorrect Database ConfigurationProvide an explicit database configuration in application.properties or application.yml.
Profile Activation ProblemsActivate the correct profile to use appropriate configurations.
Application Context IssuesVerify component scanning and context initialization are properly configured to include necessary beans.

By understanding and addressing these areas, you can efficiently manage and rectify the "Cannot determine embedded database driver class for database type NONE" error in your Spring Boot applications. This understanding ensures more robust application configuration management, ultimately leading to a smoother development experience.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design