首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

getSession() or getHibernateTemplate.getSession()

2012-10-08 
getSession() or getHibernateTemplate.getSession()?Im unsure of which of the above to use in our DA

getSession() or getHibernateTemplate.getSession()?
I'm unsure of which of the above to use in our DAOs (which extend HibernateDAOSupport).

The SpringFramework API doc on HibernateTemplate.getSession() is thus:
protected Session getSession()
Return a Session for use by this template.
Returns a new Session in case of alwaysUseNewSession" (using the same JDBC Connection as a transactional Session, if applicable), a pre-bound Session in case of "allowCreate" turned off, and a pre-bound or new Session else (new only if no transactional or otherwise pre-bound Session exists).

The SpringFramework API doc on getSession() is thus:
protected final Session getSession()
   throws DataAccessResourceFailureException,
   IllegalStateException
Get a Hibernate Session, either from the current transaction or a new one. The latter is only allowed if the "allowCreate" setting of this bean's HibernateTemplate is true.

Note that this is not meant to be invoked from HibernateTemplate code but rather just in plain Hibernate code. Either rely on a thread-bound Session (via HibernateInterceptor), or use it in combination with releaseSession.

In general, it is recommended to use HibernateTemplate, either with the provided convenience operations or with a custom HibernateCallback that provides you with a Session to work on. HibernateTemplate will care for all resource management and for proper exception conversion.

热点排行