This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate static SessionFactory

 
Jim Baker
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jim Baker
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Baker
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Glen Cai
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Elizabeth King
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic