Spring Boot + H2 + JPA
Okay people, I always say that “good is the one documented because reference will always knock on your knowledge door!”. Therefore let us talk about data persistence in spring boot using H2 and JPA.
For some time after starting off with spring boot, I could not tell the difference and why or when to use which. For that matter, allow me get you out of the confusion.
H2 database is an in memory database while JPA is an access layer. In memory DDs are those that start when the application starts and get destroyed when the application execution ends. Persistent DBs are those that exist even after the application execution is long gone.
That is simply it.
Now, when you specify H2 as the database for your spring boot application; spring boot will auto-configure it when your application is running even without defining any properties in the properties file. The default username is sa and the password is empty. So running this kind of application won’t give you any challenges.
You will get the below console.
You might again ask; how does spring boot auto-configure H2 database?- it goes back to the dependencies in the POM file. Once Spring Boot sees H2 dependency in the pom, it automatically configures data source similar to the below snippet:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
It knows you are using an in memory database (H2) therefore configures it for you.
Wait until you specify the data source to be JPA and you fail to do anything in the properties file.
You will have the visitor below on your console:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded
datasource could be configured.
Reason: Failed to determine a suitable driver class
The error says that the application cannot configure data source. This is because of spring boot’s auto-configuration advantage. What is happening is that is tries to configure the data source for you since you specified JPA in the initializer.
Good. What is the way forward now?
The solution is very simple. You need to tell your application
where your database is
how login in
the class name to you.
This is done in the properties file. The reason this is happening is because JPA persistence supports a lot of database and does not come with them like in H2 lightweight database. You therefore need to tell it what type of database you are using through the class name and where it is. Also, give it credentials to log into the database. That way you visitor (error) is sent parking.
For JPA, data is persisted. Meaning that even after the application is closed or refreshed you can still get your data.
However, you can as well make H2 persist data especially to avoid loss during refresh by saving that data to some file. It you want more on handling H2 I recommend you read https://www.baeldung.com/spring-boot-h2-database
You will learn a lot including how to access the console.
Thanks friends, remember; lets keep on documenting.