1. getCurrentSession创建的session会和绑定到当前线程,而openSession不会。
  2. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭。
  3. 这里getCurrentSession本地事务(本地事务:jdbc)时要在配置文件里进行如下设置
    如果使用的是本地事务(jdbc事务)
     <property name="hibernate.current_session_context_class">thread</property>

    如果使用的是全局事务(jta事务)

     <property name="hibernate.current_session_context_class">jta</property>
  4. getCurrentSession () 使用当前的session,openSession() 重新建立一个新的session
  5. 在一个应用程序中,如果DAO 层使用Spring 的hibernate 模板,通过Spring 来控制session 的生命周期,则首选getCurrentSession ()。
  6. 在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在 getCurrentSession() 被调用的时候,实际被执行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前 Session 为空, currentSession 会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法。

openSession()与getCurrentSession()的区别的更多相关文章

  1. Hibernate之openSession与getCurrentSession的区别

    openSession 与 getCurrentSession的区别(1)openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定 ...

  2. Hibernate中openSession() 与 getCurrentSession()的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...

  3. openSession 与 getCurrentSession的区别

    1.openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定的session对象 package cn.kiwifly.view; ...

  4. hibernate中openSession()跟getCurrentSession()方法之间的区别

    Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...

  5. hibernate3.6-联合主键注解以及openSession和getCurrentSession区别

    [联合主键]>>>>配置方式:xml:    1. Student中单独创建StudentPk主键实体类 2. 配置: <composite-id name=" ...

  6. openSession和getCurrentSession的比较

    在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = n ...

  7. sessionFactory中的openSession和getCurrentSession的一些注意事项

    今天进行Hibernate测试时遇到了一个问题 我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常: Exception in thr ...

  8. HIbernate中openSession和getCurrentSession

      这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题.   openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...

  9. SessionFactory的openSession与getCurrentSession区别

    SessionFactory 1 用来产生和管理sesssion 2 通常情况下,每个应用只需要一个SessionFactory,除非要访问多个数据库的情况 3 openSession()与openS ...

随机推荐

  1. java util.Date和sql.Date转换(时区转换)

    public static Timestamp zoneTtime(String time) throws Exception{ time= "2018-08-01T10:01:21.905 ...

  2. Navicat for MySQL使用手记(上)--创建数据库和表

    在管理MySQL数据库的图形化工具中,最为熟知的就是phpMyAdmin和Mysql-Front了,今天跟大家分享另外一个管理mysql数据库的另外一个利器---Navicat MySQL. Navi ...

  3. mongodb启动时报错ERROR: child process failed, exited with error number 1

    不多说,直接上干货! 前期博客 Ubuntu14.04下Mongodb安装部署步骤(图文详解) Ubuntu16.04下Mongodb安装部署步骤(图文详解) root@zhouls-virtual- ...

  4. mybatis由浅入深day02_6延迟加载_延迟加载总结

    6 延迟加载 6.1 什么是延迟加载 需要查询关联信息时,使用mybatis延迟加载特性可有效的减少数据库压力,首次查询只查询主要信息,关联信息等用户获取时再加载. resultMap可以实现高级映射 ...

  5. GIS-011-Cesium 使用 IIS设置

    .terrain Content-Type='application/octet-stream'

  6. linux 安装 nodejs

    原文地址:https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora 1)定位到nodejs的官方源(如果直 ...

  7. 在(MRv1)中JobTracker工作方式

    在 Hadoop MapReduce 中,JobTracker 具有两种不同的职责: 管理集群中的计算资源,这涉及到维护活动节点列表.可用和占用的 map 和 reduce slots 列表,以及依据 ...

  8. Android 5.0 API新增和改进

    开始开发 要构建 Android 5.0 版应用,您必须先下载 Android SDK,然后使用 SDK 管理器下载 Android 5.0 SDK 平台和系统映像. 更新您的目标 API 级别 要进 ...

  9. MITE

    .,text/html .,text/h323 .aaf,application/octet-stream .aca,application/octet-stream .accdb,applicati ...

  10. Android Studio 解决Fetching android sdk component information加载过久问题

    extends:http://www.cnblogs.com/sonyi/p/4154797.html 安装完成后,如果直接启动,Android Studio会去获取 android sdk 组件信息 ...