Hi,
DAOs abstract client code from making (Hibernate) specific data access calls.
For long sessions, however, it looks like there is no abstraction from storing the Hibernate session in e.g. the Web session context.
Assuming a requirement for supoprting different persistence frameworks, wouldn't it make sense to add such an abstraction level too ?
I am thinking of:
- Hibernate Util keeps the thread local variable.
- Add a thin, Hibernate-independent layer on top of Hibernate Util. Let's call this layer MyPersistenceManager.
- MyPersistenceManager has, amongst others, methods reconnect(), and disconnect(). These are Hibernate independent.
- Upon thread termination, MyPersistenceManager.disconnect() will save the thread-local variable from HibernateUtil into a MyPersistenceManager instance variable. This variable is not visible to client code. It will be saved when MyPersistenceManager is saved, e.g. into the Web session context by a servlet filter.
- Upon starting a new thread, MyPersistenceManager.connect() is called by e.g. a servlet filter. MyPersistenceManager.connect() will hand-over the MyPersistenceManager instance variable back to HibernateUtil, via HibernateUtil.reconnect(session). The MyPersistenceManager instance variable will be nullified.
Note: we are currently targeting at Hibernate 3.0. When adopting Hibernate 3.1 later, it looks like the TransactionInterceptor can be useful; however this only supports session per request ?
Regards,
- Fernand.
|