0

We have a Java-Hibernate based web application in which we deal with PostgreSQL database.(It is a single database having multiple schema(s) in it.)

Currently we are facing scalability issues so we have decided to split our schema(s) among multiple database.

Example:

  • Previously we have database called "mydb".
  • And now we split database like "mydb_1", "mydb_2", "mydb_3", so on... (As load will increase, we will create new database.)

Problem:

  • How to manage multiple database from one *.cfg.xml? (We do not want to introduce multiple *.cfg.xml (for every database) every time.)
  • Can this be achieved with newer hibernate version? (We are ready to upgrade our Hibernate version)
  • Do we need to create pool of session factory? (I think its a bad idea and thought... :) )

Note: It would be great, if anyone can provide tutorial/document link for this.

Similar Question: Hibernate using multiple databases

(I am expecting similar kind of answer but without multiple cfg.xml :( )

4
  • 1
    How are you creating the session factory at the moment (what library to you use) ? it is spring framework ? Commented Mar 22, 2017 at 12:53
  • :( Its a J2EE (non-spring based application) and currently we use hibernate3.jar for this. (However if spring is the only solution...then we can go for it also. Can this be achieve in Spring based application? ) Commented Mar 22, 2017 at 13:04
  • (I am expecting similar kind of answer but without multiple cfg.xml :( ) : look at the most upvoted answer in the question you provided : stackoverflow.com/a/5982535/201557 : it is using a single config file. Commented Mar 22, 2017 at 13:28
  • But he has mentioned that "there should be different cfg.xml files for different databases" . In my case, we might need to create database runtime. And adding *.cfg.xml for new database require downtime of server. So we do not want to go for this solution. Commented Mar 22, 2017 at 13:32

1 Answer 1

1

Ok, so your requirement is not

only one hibernate config file

but to be able to

create new sessionFactory without stopping the server.

The code in the question you referenced is looking like :

Configuration config = new Configuration().configure("<complete path to your cfg.xml file>");
SessionFactory sessionFactory = config.buildSessionFactory();

AS you can see you have a config object first, before getting a sessionFactory. Just fiddle with it in the code before buiding the session factory :

Configuration config = new Configuration().configure("<complete path to your cfg.xml file>");
config.setProperty("datasource", new_datasource_to_the_new_database)
SessionFactory sessionFactory = config.buildSessionFactory();
  • replace the "datasource" key by the actual key you are using in the current hibernate.cfg.xml file so that is overrides it. Build new data source, the same way you are building it currently, pointing to the new database.
  • keep the session factory in an accessible location
  • When doing treatment, choose the the right sessionFactory and get a session from there.
Sign up to request clarification or add additional context in comments.

1 Comment

Thierry, I appreciate your help. Thanks for the answer. I am trying as you have suggested...once I will complete, I will revert you.

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.