专栏首页jeremy的技术点滴两种获取hibernate会话的区别

两种获取hibernate会话的区别

今天在工作中,发现用Hibernate实现的DAO类中存在两种获取hibernate会话的方式,如下:

@Repository("demoDao")
public class DemoDaoImpl extends HibernateDaoSupport implements DemoDao{
    //通过getSession方法获取
    @Override
    public Demo method1(final Integer param) {
        Session session = this.getSession();
        ...
    }

    //通过getHibernateTemplate().execute方法获取
    @Override
    public Demo method2(final Integer param) {
        return getHibernateTemplate().execute(new HibernateCallback<Demo>() {
            @Override
            public AppPzTestStats doInHibernate(Session session) throws HibernateException, SQLException {
                ...
            }
        });
    }
}

研究了下,这两种方式还有有区别的,先看看javadoc。

/**
 * Obtain a Hibernate Session, either from the current transaction or
 * a new one. The latter is only allowed if the
 * {@link org.springframework.orm.hibernate3.HibernateTemplate#setAllowCreate "allowCreate"}
 * setting of this bean's {@link #setHibernateTemplate HibernateTemplate} is "true".
 * <p><b>Note that this is not meant to be invoked from HibernateTemplate code
 * but rather just in plain Hibernate code.</b> Either rely on a thread-bound
 * Session or use it in combination with {@link #releaseSession}.
 * <p>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.
 * @return the Hibernate Session
 * @throws DataAccessResourceFailureException if the Session couldn't be created
 * @throws IllegalStateException if no thread-bound Session found and allowCreate=false
 * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean)
 * @deprecated as of Spring 3.2.7, in favor of {@link HibernateTemplate} usage
 */
@Deprecated
protected final Session getSession() throws DataAccessResourceFailureException, IllegalStateException {
	return getSession(this.hibernateTemplate.isAllowCreate());
}

/**
 * Execute the action specified by the given action object within a
 * {@link org.hibernate.Session}.
 * <p>Application exceptions thrown by the action object get propagated
 * to the caller (can only be unchecked). Hibernate exceptions are
 * transformed into appropriate DAO ones. Allows for returning a result
 * object, that is a domain object or a collection of domain objects.
 * <p>Note: Callback code is not supposed to handle transactions itself!
 * Use an appropriate transaction manager like
 * {@link HibernateTransactionManager}. Generally, callback code must not
 * touch any {@code Session} lifecycle methods, like close,
 * disconnect, or reconnect, to let the template do its work.
 * @param action callback object that specifies the Hibernate action
 * @return a result object returned by the action, or {@code null}
 * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
 * @see HibernateTransactionManager
 * @see org.hibernate.Session
 */
<T> T execute(HibernateCallback<T> action) throws DataAccessException;

从文档上看,getSession的方式得到的Session需要由程序员自行调用releaseSession方法进行session的释放,而且getSession方法已经不推荐使用了。官方更推荐使用hibernateTemplate配合HibernateCallback的方案。这种方案由hibernate负责处理资源的管理及异常的转换。

另外看到网上一哥们的分析,于是更坚信了要使用hibernateTemplate配合HibernateCallback的方案。一搜项目,竟然有700多处都是老写法,看来得花一番功夫将所有这些代码改成推荐的方案了。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Posgresql高可用集群实践

    工作中一个第三方软件使用了Posgresql数据库,而在我们的场景里,我们需要保证Posgresql数据库的高可用,网上查找了一下,发现stolon这个高可用,...

    jeremyxu
  • 初识glusterfs

    jeremyxu
  • 使用dex搭建一个身份认证系统

    一个成熟的软件系统一般必须有一个可靠的身份认证与权限验证功能。这一块要自研快速实现还是需要花费挺多精力的,幸好开源领域目前已经有不错的解决方案,一般拿过来按项目...

    jeremyxu
  • CodeForces 24D Broken robot (概率DP)

    D. Broken robot time limit per test 2 seconds memory limit per test 256 me...

    ShenduCC
  • DAY2:阅读CUDA C Programming Guide之编程模型

    GPUS Lady
  • HTML4.01规范-文本(3)

    Note. The following section is an informative description of the behavior of som...

    py3study
  • Alibaba开源Java诊断工具Arthas简单介绍

    Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱。在线排查问题,无需重启;动态跟踪Java代码;实时监控JVM状态。

    yuanfan2012
  • uva----(10794) A Different Task

     A Different Task  The (Three peg) Tower of Hanoi problem is a popular ...

    Gxjun
  • ZOJ 3605 Find the Marble(dp)

    Find the Marble ---- Time Limit: 2 Seconds      Memory Limit: 65536 KB ---- Alic...

    ShenduCC
  • 【CodeForces 489A】SwapSort

    In this problem your goal is to sort an array consisting of n integers in at mos...

    饶文津

扫码关注云+社区

领取腾讯云代金券