Monday, August 5, 2019

Integrate H2 Database In Spring Boot

How to Integrate H2 database in Spring Boot App?

Q- What is the H2 database?
  • H2 is one of the most popular in-memory databases written in Java.
  • It is an Open Source, lightweight i.e around 1 MB only and Very fast in-memory database.
  • It Supports Web Console like other databases.
  • And Supports Standard SQL and JDBC API
It is called in-memory database because it is created database while application start and gets destroyed on stopping or shutdown the application.

Spring Boot provides excellent integration support for H2 using simple properties configuration.

Q- What is the use of h2 database? Or Why in memory(H2) database? Or How does h2 database work?

H2 is an open-source lightweight Java database. It can be embedded with Java applications or run in the client-server mode.
Mainly, H2 database can be configured to run as in-memory database, which means that data will not persist on the disk.
It is very useful in testing while we don't want to make any changes into real database. Or you are doing some POC(Proof of concept) before starting a your project or you are working on prototype. and we don't want to set up an actual database.

Q- What is H2 Console?
H2 provide support to see your database on web browser. or we can say that H2 console helps to access the database from a browser.

Q- How do I download H2 Database in spring boot application?
you don't need to download and setup this. you just need to add dependency for H2 Databse in pom.xml. Spring will Auto Configure the same.

Q- How do I restore my h2 database?
One you restart you application it will restart automatically. because it is created database while application start and gets destroyed on stopping or shutdown the application.

Q- How does h2 Database connect to spring boot application?

Go to https://start.spring.io/ to generate spring boot project with following.
Choose Group like com.javaiq.springboot.example
Choose Artifact like spring-boot-h2-database-jpa-hibernate
Choose following dependencies
  • Web
  • JPA
  • H2
  • DevTools
download the spring boot project and inport into Eclipse.
Go to : File -> Import -> Existing Maven Project.

Note:- you will see as below dependency in your pom.xml

<dependency>
 <groupId>com.h2database</groupId>
 <artifactId>h2</artifactId>
 <scope>runtime</scope>
</dependency>
H2 provides a web interface called H2 Console to check your data. To enable h2 console add as given below into the application.properties.
/src/main/resources/application.properties

# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2

Add Datasource into application.properties

# Datasourde details 
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
http://localhost:8080/h2-console
Or
http://localhost:8080/h2/

How to use H2 in unit testing:
First of all you need to add the dependencies for your database driver like if you are using ( MYSQL Database) then make the dependency for h2 test scoped.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>

No comments:

Post a Comment