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.
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.
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.
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.
Thank you for suggesting persistence.xml. I’m able to have both db alongside each other. I need one more clarification and another suggestion.
(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.)?
(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?