Question
What are the steps to resolve 'Cannot determine embedded database driver class for database type NONE' error when using Spring Boot with JPA?
@Configuration
@EnableJpaRepositories("demo.core.entity")
@EnableTransactionManagement
class JpaApplicationConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setPersistenceUnitName("transactions-optional");
return factory;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}
}
Answer
The error 'Cannot determine embedded database driver class for database type NONE' typically occurs in Spring Boot applications when the application is unable to auto-configure a data source for JPA. This can happen if the necessary database driver dependencies are missing or not configured correctly in your project.
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
Causes
- Missing database driver dependencies in the project's build configuration (e.g., Maven or Gradle).
- Configuring Spring Data JPA without specifying a database (e.g., H2, MySQL) leads to Spring assuming there is no embedded database available.
- Your application might not have the correct dependency versions for DataNucleus and other libraries.
Solutions
- Add the appropriate database driver dependency in your POM.xml or build.gradle file, such as: ```xml <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> ```
- Ensure your application.properties or application.yml file specifies the data source configuration. For example: ```properties spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= ```
- If you're using Google App Engine, ensure you have followed the correct setup for DataNucleus as per Google App Engine documentation.
Common Mistakes
Mistake: Not including the necessary database driver dependency.
Solution: Make sure to add the correct database driver to your Maven or Gradle dependencies.
Mistake: Configuring JPA without setting data source parameters in properties files.
Solution: Specify the datasource properties in your application.properties or application.yml.
Mistake: Using incorrect versions of dependencies that conflict with Spring Boot.
Solution: Check for compatibility between your Spring Boot version and other dependencies.
Helpers
- Spring Boot
- embedded database
- JPA configuration
- DataSource
- Spring Data JPA
- H2 database
- DataNucleus
- Google App Engine