4

My question is very simple. and when I search on StackOverFlow I get several answers. But really I was not satisfied with them.

Q. What we can create more than one sessionFactory in Hibernate. Ans And Its true We can create.As in my app i am able to da same.

Q.Now question arise why we should not create more than one session factory in an app.?? and what are the merits and demerits of having more than one session factory.

Thanks Guys

7
  • 5
    The only merits to having many SessionFactory beans is if you have many data sources. If you only have one data source, the one SessionFactory is enough. Commented Mar 31, 2014 at 18:33
  • 1
    what will demerits if we have multiple sessionfactory for single database...... Commented Mar 31, 2014 at 18:34
  • 3
    Wasted memory. Anything you can do with those multiple objects you can do with a single one. Commented Mar 31, 2014 at 18:36
  • If its true. whats the purpose of hibernate team, not to have it as singleton.......??? Commented Mar 31, 2014 at 18:38
  • 1
    Why singletons are evil. They are usually singletons in the scope of a context. Commented Mar 31, 2014 at 18:39

3 Answers 3

4

Why only one sessionFactory object per database in Hibernate ?

Here we can explain what is SessionFactory..

• SessionFactory is an interface, which is available in “org.hibernate” package.

• Session factory is long live multithreaded object.

• Usually one session factory should be created for one database.

• When you have multiple databases in your application you should create multiple SessionFactory object.

• Assume the scenario that you are using one database called mysql in your application then following is the way to create the SessionFactory object :

Configuration cfg=new Configuration(); // Empty object will be created.

cfg=cfg.configure();

Here when you called configure() method It looks for hibernate-cfg.xml and for Hibernate mapping file filled with all the properties defined in the configuration documents and mapping documents.

SessionFactory sc=cfg.buildSessionFactory();

• SessionFactory object will be created once and will be used by multiple users for long time. • Session Factory object is the factory for session objects.

If you are using two databases called mysql and oracle in your hibernate application then you need to build 2 SessionFactory objects:

Configuration cfg=new Configuration();

Configuration cfg1=cfg.configure(“mysql.cfg.xml”);

SessionFactory sf1=cfg1.builed SessionFactory();

Configuration cfg2=cfg.configure(“oracle.cfg.xml”);

SessionFactory sf2=cfg2.builed SessionFactory();

When we are using more than one database in our application than we use the HibernateUtil class which is implemented based on singleton design pattern which insures that one and only one sessionFactory object will be created for entire application.Session factory objects are to be implemented using the singleton design pattern. Instances of SessionFactory are thread-safe and typically shared throughout an application. Because creation of a SessionFactory is an extremely expensive process which involves parsing hibernate configuration/mapping properties and creating database connection pool .Creating a database connection pool requires establishing database connections (i.e creating Connection objects) which has overhead due to the time taken to locate the DB server. So if you create a SessionFactory for every request , it implies that you are not using database connection pool to serve your request. So creating number of instances will make our application heavy weight. But the session objects are not thread safe. So in short it is - SessionFactory objects are one per application and Session objects are one per client.

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

1 Comment

Please go through it
0

It is always recommended that there is one session factory per database, per JVM. Having multiple instances of session factory can lead to performance problems as well as abnormal behavior of transactions.

Comments

0

When you have multiple database instances used in your application you will need multiple hibernet.cfg.xml file (if you use xml based configuration) , where in each hibernet.cfg.xml file you will have to mention separate tag which will consist the database configurations.

But it is really not recommend to have multiple instances of session factory unless have any stern business requirement or need, because it will degrade the application performance.

Comments

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.