2

I'm trying to get start with spring-boot integration with databases.

I have read official 29.1 Configure a DataSource topic and it is pretty clear for me.

I want to publish my application with Heroku so I've take a look on official sample project configuration.

Part of pom.xml:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    //...
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
    </dependency>
    //...
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1203-jdbc42</version>
    </dependency>
    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
        <version>3.4.1</version>
    </dependency>

I'm wondering now, what is a purpose of using in-memory database like hsqldb with normal production-like database (postgres here)? What is a case when it can be usefull?

What is more, how this will work?

EDIT

Ofc, I have missed something in case of sample project I have mentioned. Comment in application.properties explain why hsql is used:

# Database Config
# Un-comment these lines to connect to a database. When commented out, you automatically get an in-memory-database.
#spring.jpa.hibernate.ddl-auto=update

2 Answers 2

3

I had 2 cases for that:

  1. Testing purpose - you can use it when you are doing automated testing so you don't need to setup a "real" production like database, but rather have something that will setup hsql as such and populate it - can be a huge saving in resources

  2. In-memory caching - In situation when you don't only need only key-value caching, but somethign more flexible like extraction based on filter (multiple keys which can be combined in different way) HSQL is great for it, since you can cache everything (or part of it) in hypersql and extraction from it is way faster then from "real" database. Also, you can use it to cache some business logic results based on data from database and provide those results from HyperSQL to any request - this can be a huge saving in processing time

Sign up to request clarification or add additional context in comments.

Comments

1

There could be several reasons but really its up to how you define your architecture/needs:

The inMem DB's are much quicker accessing data and you could use it to store stuff like temporary data, user session information, etc.

The db's like postgres could be used as a complement (again, depending on what you want/need) to store more "static", least used information or simply data persistence.

Again, depending on the scenario you could want one, both or none.

Hope it helps, Sérgio

3 Comments

really helpful. Thank you, however I need to ask - how it will works in an example I have posted in question? As i can see there is non stuff like cache, user session etc - just normal JPA repository. So, why there is a hsql db?
Doing a quick search.. looks like hsqldb as a default but in the example it was changed to postgres (from the src looks like its the only being used).
I would need to compile and test (cant do it now.. at work).. but if you comment that dependency it should work (again.. not 100% sure about it.. test it out)... check: devcenter.heroku.com/articles/… "By default, the generated app sets up the Hypersonic in-memory database. However, it is strongly recommended to use the same database locally as in production. So we will switch the application over to using Postgres with this Roo command:"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.