Hibernate in memory database alongwith actual database


#1

Hello all,

I came to know about Hibenate in-memory database while practicing the tutorial provided by Hibernate.
In my situation, I have an actual database but for processing some data which doesn’t need to be stored in database, I’m using a lot of HashMap (Java Collections) right now. I would like to replace HashMap with in-memory database keeping the actual database intact.

How and where should I configure in-memory database only for using during data processing.

Thank you.


#2

Unfortunately, it’s not clear what you are asking. You can use an in-memory database like H2 with Hibernate and is rather straightforward.


#3

Hello @vlad,

Sorry for not being clear. We are using Spring boot framework for our project which uses “application.properties” for storing application wide configuration settings. The database configuration settings is like this as of now.

spring.datasource.url = jdbc:mysql://localhost:3306/ca_db2
spring.datasource.username = dev_user
spring.datasource.password = Qadev@123

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
...

and so on.
Now I wanted to know how to have Hibernate in-memory database along with “ca_db2” database which is being used for persistent data.

I found a solution by creating persistence.xml file inside

/src/main/resources/META-INF/persistence.xml

and kept all in-memory database related configuration like this.

<properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />

            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>

Thank you.


#4

Why would you want to have both? The Hibernate SessionFactory can use one and only one DataSource.

In your case, you should use separate profiles for in-memory testing and production environements.


#5

Because, as I’ve mentioned in my original question, I’m not using in-memory for “testing” purpose.
Some data need to be processed by updating 3 to 4 times, before sending the result finally back to frontend.

Right now I’m using HashMaps (from Java collections framework) to store the data temporarily. but HashMap can only retrieve data by using key. It’s not possible to retrieve data based on any other parameters of an object.
example: I have a HashMap<id, Person> personHashMap = new HashMap<>();
For retrieving person where id=2, I could get data as personHashMap.get(2);

But how to retrieve person by name from that HashMap? And I don’t need the updated Person object to store in DB. I just want to keep a list of Person objects for certain time and discard the memory.

This is the reason I need in-memory database along with Persisting database.
Let me know for any more clarificaiton.

Thank you.


#6

This is a very unusual requirement for using Hibernate. Nevertheless, you can use multiple EntityManagerFactories. One EntityManagerFactory can use the in-memory DB while the other one can use MySQL.

So, you need to have multiple persistence.xml files as well:

  • persistence-h2.xml for H2
  • persistence.xml for MySQL

Just make sure you bootstrap both EMFs using the right persistence.xml file.


#7

Hello @vlad,

Thank you for suggesting persistence.xml. I’m able to have both db alongside each other. I need one more clarification and another suggestion.

  1. (Clarification) I would like to know why do you think this is an unusual requirement given the fact that I get different kind of information about an object from 3 different sources. And for updating the object from querying on different attributes of an object as I’ve explained above.
    OR
    Can you point me out at any other way of achieving this without in-memory database in Java (I know this question is not relevant to this forum, but it would be helpful if you know of any such feature in Java.)?

  2. (Suggestion) If I would like to go with in-memory database itself, I need different instance of the database to be created with separate memory block and table, each time a class is instantiated. Is it possible to have new instances of database each time? I’m asking this question with respect to DFP API.
    eg: By considering DFP API, I’ll get different information about Line Item object from, Order object, Reports service object and the Line Item Object itself.
    Depending on the user’s query, each time some amount of Line Items are fetched from DFP and will be processed inside in-memory database. And at the same time other users will be also querying Line Items from DFP and when I fetch another set of LineItems, I need to store and process them in a separate table to avoid mixing the data of first user and second user and etc.

Basically what I need is, since the database connection is open inside a class and for every different user’s request, a new class is instantiated, at the same time, I need a new in-memory database or table to process data related to that particular user.
Is it achievable?

Thank you.


#8

I don’t really understand your application design or requirements so I can’t give you any suggestion.

However, you should tackle this issue in your team as you have the best knowledge of your domain model.