Hibernate中的GetCurrentSession()方法
从3.0.1版本开 始,Hibernate增加了SessionFactory.getCurrentSession()方法。
采用getCurrentSession()创建的session在commit或rollback时会自动关闭,如果commit()之后再关闭,就会报session已经关闭的错误;但是如果不commit()而执行关闭,虽然不会报什么错误,但是这时观察数据库添加数据是没有成功的,而openSession必须手动关闭。
在一个应用程序中,如果DAO 层使用Spring来控制session 的生命周期,则首选getCurrentSession()。
在 SessionFactory 启动的时候,Hibernate 会根据配置创建相应的 CurrentSessionContext,在 getCurrentSession()被调用的时候,实际被执行的方法是 CurrentSessionContext.currentSession()。在 currentSession() 执行时,如果当前Session 为空,currentSession 会调用 SessionFactory的openSession。
getCurrentSession配置:
如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
Hibernate中的GetCurrentSession()方法的更多相关文章
- hibernate中的merge()方法
Hibernate提供有save().persist().savaOrUpdate()和merge()等方法来提供插入数据的功能.前三者理解起来较后者容易一些,而merge()方法从api中的介绍就可 ...
- Hibernate-ORM:09.Hibernate中的getCurrentSession()
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将讲述,以优雅的方式创建session对象,我将会说明优点,并提炼成工具类 优点: 1.无需手动关闭s ...
- spring中使用Hibernate中的getCurrentSession报出:createQuery is not valid without active transaction
1.错误信息 HTTP Status 500 - createQuery is not valid without active transaction type Exception report m ...
- Hibernate中Session.save()方法的返回值是什么
public Serializable save(Object object) Parameters: object - a transient insta ...
- Hibernate中易错地方的总结
1.Hibernate中的配置文件要放在src下,注意不能放在包目录下 2.Hibernate中@Before @After方法不能再普通的类里用,只有在专门的JUnit测试用例里面用. 3.使用 ...
- Hibernate中的HQL语言
一.HQL语言简介 HQL全称是Hibernate Query Language,它提供了是十分强大的功能,它是针对持久化对象,直接取得对象,而不进行update,delete和insert等操作.而 ...
- hibernate中openSession()跟getCurrentSession()方法之间的区别
Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...
- Hibernate中openSession() 与 getCurrentSession()的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...
- 关于hibernate中的session与数据库连接关系以及getCurrentSession 与 openSession() 的区别
1.session与connection,是多对一关系,每个session都有一个与之对应的connection,一个connection不同时刻可以供多个session使用. 2.多个sessi ...
随机推荐
- JavaScript学习笔记——对象基础
javascript对象基础 一.名词解释: 1.基于对象 一切皆对象,以对象的概念来编程. 2.面向对象编程(oop Object oriented programming) A.对象 就是人们要研 ...
- Web Api系列教程第2季(OData篇)(一)——OData简介和一个小应用
第一季的链接以及系列导航:http://www.cnblogs.com/fzrain/p/3490137.html 在这里,首先要感谢Taiseer Joudeh不断的为我们带来最新的技术分享,楼主对 ...
- 端口扫描(TCP)
还待优化... #include <string.h> #include <WinSock.h> #include <stdio.h> #pragma commen ...
- 入门:HTML表单与Java 后台交互(复选框提交)
仅仅给出部分关键代码: HTML form code: <form action="JavaFormTest" method="post" name=&q ...
- C/C++宏中#与##的讲解
http://www.cnblogs.com/morewindows/archive/2011/08/18/2144112.html
- centos 7.0 PHP 5.6.5 安装过程 (php+nginx)
php网址 http://php.net/downloads.php 首先下载 php-5.6.5.tar.gz [root@localhost src]# wget http://cn2.php.n ...
- GoLang之方法与接口
GoLang之方法与接口 Go语言没有沿袭传统面向对象编程中的诸多概念,比如继承.虚函数.构造函数和析构函数.隐藏的this指针等. 方法 Go 语言中同时有函数和方法.方法就是一个包含了接受者的函数 ...
- apache struts 2 任意代码执行漏洞
漏洞检测地址:http://0day.websaas.cn 漏洞利用工具,如下: 漏洞利用,如下: step1 step2 step3 提权思路,如下: 1.开启虚拟终端,执行命令,但是,提示“连接被 ...
- C++ security issue analyze
https://sploitfun.wordpress.com/about-2/ “Happiness is only real when shared” – Into the wild http:/ ...
- 2015年12月01日 GitHub入门学习(二)手把手教你Git安装
序:Mac与Linux中,Mac都预装了Git,各版本的Linux也都提供了Git的软件包.下面手把手教你Windows下的安装. 一.Git Windows GUI 下载地址 msysgit htt ...