Spring Boot 2 Tips & FAQ
JPA
Issue: Field xxx in xxx required a bean named 'entityManagerFactory' that could not be found.
Symptoms:
When using Spring Boot2 with JPA, I encountered a weird issue, where if I'm using H2 as the embedded DB, everything worked fine, but once I switched to external MySQL under "prod" profile, it kept failing:
Analysis & Solutions:
This was because I excluded HikariCP
as I found it's really unfamiliar to me and didn't know what it was.
Unfortunately, this was exactly what caused the problem.
Check out the Spring Boot doc, here, and it clearly states that:
Production database connections can also be auto-configured by using a pooling DataSource. Spring Boot uses the following algorithm for choosing a specific implementation: 1. We prefer
HikariCP
for its performance and concurrency. IfHikariCP
is available, we always choose it. 2. Otherwise, if theTomcat pooling DataSource
is available, we use it. 3. If neitherHikariCP
nor the Tomcat pooling datasource are available and ifCommons DBCP2
is available, we use it. If you use thespring-boot-starter-jdbc
orspring-boot-starter-data-jpa
“starters”, you automatically get a dependency toHikariCP
.
So we either use the default by simply adding org.springframework.boot:spring-boot-starter-data-jpa
dependency without excluding HikariCP
, or explicitly adding your preferred pooling library.
Debug
Start Spring Boot App in Debug Mode
It's very easy to start Spring Boot appp in debug mode when using org.springframework.boot:spring-boot-maven-plugin
Maven plugin:
Last updated