hibernate中openSession()和getCurrentSession()的区别

1. getCurrentSession()在事务提交或回滚之后会自动关闭,而openSession()需要你手动关闭。

    如果使用openSession()而没有手动关闭,多次之后会导致连接池溢出。

2. openSession()每次会创建新的session对象,而getCurrentSession()则是使用现有的session对象。

3. 使用getCurrentSession()时,得先在配置文件hibernate.cfg.xml中做如下配置

    <!-- 配置可以使用getCurrentSession(),thread表示是本地事务(jdbc事务) -->
    <property name="hibernate.current_session_context_class">thread</property>

posted @ 2015-12-01 10:43  sky钦  阅读(122)  评论(0)    收藏  举报