• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate made easy book questions

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have read the book from Cameron McKenzie "Hibernate made easy" and it helped me a lot to understand how hibernates works, @Cameron great book .
I implemented his HibernateUtil class in my application, but I am missing some piece of information that's not in the book. The application that I am writing is a database application using MDI, this means that the user can have multiple "connections" to the database, every screen shows some data from the database that can be viewed/edited or the user can add a new record.

The question is when do I start and end a session in a particular screen?
At the beginning I have to retrieve data from the DB thus I need a session, but do I need to close this or do I close the session when the screen closes (which can be remain open for a whole day). Then I do not need to begin a new session when the user edited the data and want to save it.
If a let the session open when the screen remains open, how does this affect the other screens that also needs a session??

Also I don't see anything about a hibernate pool in the book, can anyone explain how this works?
I have already configured the c3p0 pool, in the HibernateUtil I use Cameron's approach to get a (new) session by calling factory.getCurrentSession(). Is this a good approach together with a pool or do I need to call factory.openSession().



Thanks in advance,

Marcel
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Regd Sessions:
-----------------
- Session is an abstract concept, but somewhat like a 'sequence of DB transactions'. In general, a Session should be short lived and represent the lifetime of a 'unit of work' (which is subjective and depends on the operations and flows of your application).
The Hibernate Reference manual (it'll be under the \documentation\manual\en-US directory of your Hibernate directory) Chapters 2.5 and 11 provide good reference on how to use Sessions.

- Some more tips that can help your decision:
> Sessions are not thread safe (though an MDI need not necessarily imply multiple threads by itself - depends on your application logic; all UI runs in the same Swing Event Dispatch thread for a Swing app).


- Hibernate session creation and release are lightweight operations, so performance need not be your worry. A session does not open a DB connection till some operation is done. However, session-per-operation is an antipattern.

Regd openSession and getCurrentSession:
------------------------------------------------
Hibernate 3 introduced the concept of 'contextual sessions' because it was noticed that it was very common for sessions to be opened and closed with particular 'contexts': one context could be the context of a thread, another could be the context of a JTA transaction, or it could be a custom context you define. getCurrentSession() for the various contexts also call factory openSession() internally, but they call them as appropriate to the context. If you're not satisfied with any of the predefined contexts but your sessions are opened and closed in some pattern, you can define your own context class. If there is no common pattern, then just do openSession() yourself at appropriate points in your app. So, basically, getCurentSession() is a convenience mechanism to avoid your own openSessions.

Pooling:
--------
C3P0 is for 'Connection pooling'. It's not newly introduced with Hibernate. It's associated with JDBC. The reason connection pooling is needed, is that opening a connection to a database is a costly operation in terms of time and resources (like sockets). So connections are always shared instead of frequently opening new connections. C3P0 is not the only connection pooling framework; Apache DBCP is also a very common one. Apparently, DBCP has some thread blocking and deadlock issues in heavily multithreaded environments; C3P0 doesn't (see this comparison).
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One problem in your code I can see is this:
HibernateUtil.getSession() calls SessionFactory.openSession() which creates a new Session on every call.

So if you call a sequence like


you're beginning and committing transactions on different sessions, which is wrong. Be careful about the session management.
 
Marcel van der Ven
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, Karthik.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic