In-Memory Database with Java

I had one task at work a few days go — the main idea of the task to create Table structure in the database and load data to run a certain query and then delete a table. All those things should be dynamic inside Spring MVC.

We decided to go with HSQLDB in-memory database
1. First, you need to add hsqldb libraries in your project pom.xml file

2. We can use Server class from hsqldb to start our database

3. Once database up and running. We can establish JDBC connection

1
Suranchiyev
Beknazar
102 Main st
Hyannis

4. To stop your in-memory db

It’s it. There are few things here to think while using in-memory in production environment (Not testing purposes).

  1. Synchronization. What if one thread will go and start in-memory db and then second thread will come and will try to start same db in the same port. It will fail. We used Singleton pattern to have one instance of Server and then we synchronized the whole process of starting, loading data, queering and closing db.
  2. Setting Port explicitly. What if the server machine, where your app is running port is occupied. In our server machine with other apps so it might be issue for us.

Happy Coding!

Software Developer, Java Instructor https://www.techleadacademy.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store