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).