openSession()与getCurrentSession()的区别
- getCurrentSession创建的session会和绑定到当前线程,而openSession不会。
- getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭。
- 这里getCurrentSession本地事务(本地事务:jdbc)时要在配置文件里进行如下设置
如果使用的是本地事务(jdbc事务)<property name="hibernate.current_session_context_class">thread</property>
如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
- getCurrentSession () 使用当前的session,openSession() 重新建立一个新的session
- 在一个应用程序中,如果DAO 层使用Spring 的hibernate 模板,通过Spring 来控制session 的生命周期,则首选getCurrentSession ()。
- 在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在 getCurrentSession() 被调用的时候,实际被执行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前 Session 为空, currentSession 会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法。
openSession()与getCurrentSession()的区别的更多相关文章
- Hibernate之openSession与getCurrentSession的区别
openSession 与 getCurrentSession的区别(1)openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定 ...
- Hibernate中openSession() 与 getCurrentSession()的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...
- openSession 与 getCurrentSession的区别
1.openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定的session对象 package cn.kiwifly.view; ...
- hibernate中openSession()跟getCurrentSession()方法之间的区别
Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...
- hibernate3.6-联合主键注解以及openSession和getCurrentSession区别
[联合主键]>>>>配置方式:xml: 1. Student中单独创建StudentPk主键实体类 2. 配置: <composite-id name=" ...
- openSession和getCurrentSession的比较
在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = n ...
- sessionFactory中的openSession和getCurrentSession的一些注意事项
今天进行Hibernate测试时遇到了一个问题 我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常: Exception in thr ...
- HIbernate中openSession和getCurrentSession
这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题. openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...
- SessionFactory的openSession与getCurrentSession区别
SessionFactory 1 用来产生和管理sesssion 2 通常情况下,每个应用只需要一个SessionFactory,除非要访问多个数据库的情况 3 openSession()与openS ...
随机推荐
- Notepad++下载需要的插件(如何在Notepad++中手动下载需要的插件)
需求说明: 下载在实际工作中需要的Notepad++插件,或者是因为Notepadd++设置的原因导致不能直接在软件中显示插件. 即手动登录到指定的链接中进行插件的下载. 操作过程: 1.以xmlto ...
- c #include "" 和 <>
<>先去系统目录中找头文件,如果没有在到当前目录下找.所以像标准的头文件 stdio.h.stdlib.h等用这个方法. 而""首先在当前目录下寻找,如果找不到,再到系 ...
- 机器学习技法之Aggregation方法总结:Blending、Learning(Bagging、AdaBoost、Decision Tree)及其aggregation of aggregation
本文主要基于台大林轩田老师的机器学习技法课程中关于使用融合(aggregation)方法获得更好性能的g的一个总结.包含从静态的融合方法blending(已经有了一堆的g,通过uniform:voti ...
- “浪潮杯”山东省第五届ACM大学生程序设计竞赛(总结贴)
第一次參加省赛有点小激动,尽管是作为打星队參赛,但心情却是上下起伏. 5月9号晚上11点多到威海,有点略冷.可是空气比淄博好多了,大家到了旅馆的时候都非常晚了,抱怨了一下三星级的酒店的待遇,喝杯咖啡早 ...
- PHP-007(转)
今天安装ecshop的时候最上面出现了一个错误提示:Strict Standards: Only variables should be passed by reference in F:\www.x ...
- LinkedBlockingQueue(lbq)阻塞队列
最近开发中,经常使用这个类LinkedBlockingQueue,它是BlockingQueue这个子类. 并发库中的BlockingQueue是一个比较好玩的类,顾名思义,就是阻塞队列.该类主要提供 ...
- 安装DatabaseLibrary
Using pip pip install robotframework-databaselibrary From Source Download source from https://github ...
- 开源免费天气预报接口API以及全国所有地区代码[值得收藏]
国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...
- VC++ 6.0创建MFC工程时的初级备要点(二)
在看到一个MFC工程时,可能根据上一篇文章已经知道了哪些是重要的,那些是在创建工程的时候已经被初始化的!!下面介绍一下在看代码的过程的策略!!这里假设创建的基于多对话框的MFC工程名为test ...
- LeetCode - Nth Highest Salary
题目大概意思是要求找出第n高的Salary,直接写一个Function.作为一个SQL新手我学到了1.SQL中Function的写法和变量的定义,取值.2.limit查询分 页的方法. 在这个题 ...