aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate static SessionFactory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate static SessionFactory" Watch "Hibernate static SessionFactory" New topic
Author

Hibernate static SessionFactory

Jim Baker
Ranch Hand

Joined: Mar 10, 2002
Posts: 177
I saw some Hibernate sample code using static SessionFactory. But I defined SessionFactory as a non-static class member variable, and it worked fine.
Does static SessionFactory give me better performance? I know SessionFactory is responsible for getting JDBC connections from the DataSource.
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
static way of SessionFactory implementation is utilizing a singleton pattern, which produces only one instance of SesseionFactory in the JVM, without static, each time you ask for a SessioniFactory, you will have a new instance. If only one instance works, why do we need multiple instances. is it wasting memory?


Do you know why this cup is useful? Because it is empty.
Jim Baker
Ranch Hand

Joined: Mar 10, 2002
Posts: 177
Originally posted by Hongli Li:
is it wasting memory?


Memory is cheap. Plus, when sessionFactory is out of scope or nulled, the
sessionFactory object will be GCed.

My concern on static SessionFactory is that it may cause DB connection problems, even memory leak. Has anyone seen this?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10268
    
169

Whenever you create a SessionFactory, it parses all the configuration files(hbm or the cfg file) which is an expensive process. Repeatedly doing this will degrade the performance of the application.


[My Blog] [JavaRanch Journal]
Jim Baker
Ranch Hand

Joined: Mar 10, 2002
Posts: 177
Originally posted by Jaikiran Pai:
Whenever you create a SessionFactory, it parses all the configuration files(hbm or the cfg file) which is an expensive process. Repeatedly doing this will degrade the performance of the application.


I think Hibernate caches the hibernate.cfg.xml information. It is only parsed once when creating the first SessionFactory object.

When you call Hibernate Session.close() method, does this implies the JDBC call Connection.close()?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Originally posted by Jim Baker:


I think Hibernate caches the hibernate.cfg.xml information. It is only parsed once when creating the first SessionFactory object.

When you call Hibernate Session.close() method, does this implies the JDBC call Connection.close()?


Yes, it is cached in the SessionFactory instance. You gc that, then you will have to parse again.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Glen Cai
Ranch Hand

Joined: Aug 24, 2002
Posts: 121
Originally posted by Jim Baker:

When you call Hibernate Session.close() method, does this implies the JDBC call Connection.close()?


According to the API document (http://www.hibernate.org/hib_docs/v3/api/). Session.close() returns the JDBC connection or null. It ends the session by releasing the JDBC connection and cleaning up, which does not necessarily mean the JDBC connection is closed. I'm not sue if the released JDBC Connection can be used as a freshly created JDBC Connection or not.


"I, a universe of atoms, an atom in the universe." - Richard Feynman
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Originally posted by Jim Baker:
Memory is cheap.


Memory is less expensive that it used to be, but it is neither free nor infinite. And it's a great pain to debug. Sadly, you often won't see problems until you're into production, and then you've got angry users on your hands.

So while premature optimization is the root of all evil, there needs to be a compelling reason to ignore standard best practices in order to do something just because you can.

A consideration in this specific question about Singleton vs. multiple Hibernate SessionFactories is that your second level cache is tied to a particular SessionFactory instance. So in addition to the overhead you're generating with constantly tearing up and tearing down your SessionFactory, you're losing the ability to cache data across transactions, and increasing your cost for lookups for your entire application.

If you want to see the rate at which creating indpenedent SessionFactories can cost you, create a test suite that sets up and tears down your SessionFactory multiple times. I've seen even a small object graph bring a system to its knees rather quickly.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Elizabeth King
Ranch Hand

Joined: Jul 11, 2002
Posts: 191
Originally posted by Stevi Deter:


A consideration in this specific question about Singleton vs. multiple Hibernate SessionFactories is that your second level cache is tied to a particular SessionFactory instance. So in addition to the overhead you're generating with constantly tearing up and tearing down your SessionFactory, you're losing the ability to cache data across transactions, and increasing your cost for lookups for your entire application.
...


How do you handle second level cache in a cluster?
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Without knowing your specific requirements, it's hard to say.

I'd start with reviewing Java Persistence with Hibernate. Looks like they recommend JBoss Cache as a general solution for a clustered caching system, but it definitely depends on your overall requirements.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Originally posted by Stevi Deter:
Without knowing your specific requirements, it's hard to say.

I'd start with reviewing Java Persistence with Hibernate. Looks like they recommend JBoss Cache as a general solution for a clustered caching system, but it definitely depends on your overall requirements.


Exactly. JBoss Cache is a great second level cache in clusters. (Caveat, I work for JBoss)

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate static SessionFactory