Deepak Agrawal
Deepak Agrawal

Reputation: 1301

multiple session factory objects for single database in an hibernate based java application

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

Upvotes: 4

Views: 14383

Answers (3)

Ashish Agrawal Yodlee
Ashish Agrawal Yodlee

Reputation: 123

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.

Upvotes: 4

Dipanshu
Dipanshu

Reputation: 43

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.

Upvotes: 0

pritam kumar
pritam kumar

Reputation: 1040

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.

Upvotes: 0

Related Questions