What is an in memory database ?
In memory database is created when an application starts up and destroyed when the application is stopped. In memory databases are often brought into the limelight while unit testing. Consider that unit tests :
- Don’t have to fail when some data/schema in the database changes
- Are likely to be able to run then in parallel – multiple developers might be running the tests in parallel
In these kinds of scenarios an in memory database provides an ideal solution.
Advantages of in memory databases :
- Zero project setup or infrastructure
- Zero configuration
- Zero maintainance
- Easy to use for learning and unit tests
Also, in memory databases are easy to integrate into Spring Boot projects – Spring Boot provides simple configuration to switch between a real database and an in memory database like H2.
H2 is one of the popular in memory databases. Spring Boot has very good integration for H2 (see here). H2 is a relational database management system written in Java. It can be embedded in Java applications or run in the client-server mode. H2 supports a subset of the SQL standard. H2 also provides a web console to maintain the database.
Spring Boot and H2
There is a little configuration needed to tie Spring Boot application with H2. In most situations just adding the H2 runtime jar into dependencies section of Maven project should be sufficient. An in-memory database is live only during the time of execution of the application. H2 provides a web interface called H2 Console to see the data. To enable H2 console developer must add the following settings to /src/main/resources/application.properties (properties file used in Spring Boot projects by default) :
# Enabling H2 Console
To define h2 console adress /h2-console must be typed after host:port combination.