Explain Codes LogoExplain Codes Logo

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

java
datasource-properties
auto-configuration
spring-boot
Alex KataevbyAlex Kataev·Aug 31, 2024
TLDR

To eliminate the Spring Boot error of database type NONE, either suitably configure your spring.datasource properties if you're utilizing a database, or exclude data auto-configurations if not using any.

For a database setup, include in your application.properties:

spring.datasource.url=jdbc:YOUR_DB:mem:testdb spring.datasource.username=YOUR_USERNAME spring.datasource.password=YOUR_PASSWORD spring.datasource.driver-class-name=org.h2.Driver

Substitute placeholders with your specific database information. If you're working database-free, incorporate in your main application class:

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) public class YourApplication { // ... main method }

This would instruct Spring not to perform an unnecessary auto-configuration of a database.

Deep-dive into Datasource Configuration

In any Spring Boot application, correctly defining datasource properties is a critical procedure towards setting up a database connection. spring.datasource.url, spring.datasource.username, and spring.datasource.password guide Spring in properly configuring the connection.

To fully harness these settings, take a deeper look into the DataSourceProperties class. This class contains an array of available parameters to tweak your datasource configurations. If you're utilizing Google App Engine (GAE) along with Spring Data JPA, confirm that these properties are aligned with GAE's specific requirements.

Dicing with Auto-configuration

While Spring Boot's auto-configuration is a boon for swift development, you must handle it prudently. If you're not utilizing a traditional database, exclude DataSourceAutoConfiguration.

For scenarios involving an in-memory database like H2, adding its dependency in your POM should suffice for Spring Boot to auto-configure. However, if your JPA setup doesn't require Spring to lend a hand, exclude HibernateJpaAutoConfiguration:

@EnableAutoConfiguration(exclude={HibernateJpaAutoConfiguration.class})

Alternatively, you can setup exclusions within your application.properties:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Pragmatic Database Setup

Investigate whether your application genuinely necessitates an embedded database. If you're dealing with external databases, adjust your datasource settings for the appropriate JDBC URL and include the relevant driver dependencies in your POM.

Pruning erroneous POM dependencies

Be sure to review your POM file attentively to ensure all dependencies align with your selected database configuration. Misconfigurations or missing dependencies could cause that dreadful "Cannot determine embedded database driver class" error.

Strategy: Exclusions or Annotations

While Java annotations provide a hands-on approach to exclude auto-configurations, handling this via application.properties could favor clarity and be more manageable in large-scale projects.

Resolving incompatible dependencies

Ensure you're utilizing a compatible driver for your database. Mismatch between the database version and the driver may lead to failures:

// Here lies Little Bobby Tables... spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Dealing with Dependency Conflicts

Post integration of a new starter or library, dependency conflicts might crop up. Specifying your Spring Boot configuration version ensures that all your dependencies play well with each other:

<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>COMPATIBLE_VERSION</version> // No error dragons here! <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

Winning strategy with Auto-configuration

Make strategic use of @SpringBootApplication and @EnableAutoConfiguration. Grasping when to engage and withdraw auto-configurations could significantly mitigate issues originating from Spring Boot's assumptions about your setup.