First level cache is only used by the current session. As soon as you close session that cache is cleared. You can also clear first level cache without closing session, by explicitly calling session.clear().
Second level cache is used for multiple sessions. If you close a session, loaded objects remain in the second level cache and may be loaded from there (instead of being loaded from the database), if a new session happens to query one of these objects again. Second level cache is bound to the SessionFactory. If you decide to use second level cache you must not modify your database other than using sessions acquired from your session factory. If you do, you risk getting a StaleObjectException. Second level cache is usually not practically usable, if you have a two tier application (cllient directly accessing database).
|