openSession和getCurrentSession的比较
在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法。
在进行配置信息管理时,我们一般进行一下简单步骤:
Configuration cfg = new Configuration(); // 获得配置信息对象
SessionFactory sf = cfg.configure().buildSessionFactory(); //解析并建立Session工厂
1. Session session = sf.getCurrentSession(); // 获得Session
2. Session session = sf.openSession(); // 打开Session
对于上述的两个方法,有以下区别:
1. openSession 从字面上可以看得出来,是打开一个新的session对象,而且每次使用都是打开一个新的session,假如连续使用多次,则获得的session不是同一个对象,并且使用完需要调用close方法关闭session。
2. getCurrentSession ,从字面上可以看得出来,是获取当前上下文一个session对象,当第一次使用此方法时,会自动产生一个session对象,并且连续使用多次时,得到的session都是同一个对象,这就是与openSession的区别之一,简单而言,getCurrentSession 就是:如果有已经使用的,用旧的,如果没有,建新的。
注意 :在实际开发中,往往使用getCurrentSession多,因为一般是处理同一个事务(即是使用一个数据库的情况),所以在一般情况下比较少使用openSession或者说openSession是比较老旧的一套接口了;
对于getCurrentSession 来说,有以下一些特点:
1.用途,界定事务边界
2.事务提交会自动close,不需要像openSession一样自己调用close方法关闭session
3.上下文配置(即在hibernate.cfg.xml)中,需要配置:
<property name="current_session_context_class">thread</property>
(需要注意,这里的current_session_context_class属性有几个属性值:jta 、 thread 常用 , custom、managed 少用 )
a).thread使用connection 单数据库连接管理事务
b).jta (java transaction api) Java 分布式事务管理 (多数据库访问),jta 由中间件提供(JBoss WebLogic 等, 但是tomcat 不支持)
下面是openSession 和 getCurrentSession 简单实例的区别 :
1.openSession方式 :
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.hibernate.model.Student; // 注意包路径
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("s1");
s.setAge(1);
Configuration cfg = new Configuration(); // 获得配置信息对象
SessionFactory sf = cfg.configure().buildSessionFactory(); //解析并建立Session工厂
Session session = sessionFactory.openSession(); // 打开Session
session.beginTransaction(); // 看成一个事务,进行操作
session.save(s); // 会找到 Student 这个类,寻找set方法
session.getTransaction().commit(); // 提交对数据的操作
session.close();
sf.close();
}
}
2.getCurrentSession方式 :
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.hibernate.model.Student; // 注意包路径
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("s1");
s.setAge(1);
Configuration cfg = new Configuration(); // 获得配置信息对象
SessionFactory sf = cfg.configure().buildSessionFactory(); //解析并建立Session工厂
Session session = sessionFactory.getCurrentSession(); // 打开Session
session.beginTransaction(); // 看成一个事务,进行操作
session.save(s); // 会找到 Student 这个类,寻找set方法
session.getTransaction().commit(); // 提交对数据的操作
sf.close();
}
}
Student 类代码 :
package com.hibernate.model;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
openSession和getCurrentSession的比较的更多相关文章
- hibernate中openSession()跟getCurrentSession()方法之间的区别
Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...
- Hibernate中openSession() 与 getCurrentSession()的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...
- sessionFactory中的openSession和getCurrentSession的一些注意事项
今天进行Hibernate测试时遇到了一个问题 我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常: Exception in thr ...
- Hibernate之openSession与getCurrentSession的区别
openSession 与 getCurrentSession的区别(1)openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定 ...
- HIbernate中openSession和getCurrentSession
这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题. openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...
- hibernate3.6-联合主键注解以及openSession和getCurrentSession区别
[联合主键]>>>>配置方式:xml: 1. Student中单独创建StudentPk主键实体类 2. 配置: <composite-id name=" ...
- Hibernate关于openSession和getCurrentSession的理解
来源(转载):http://blog.csdn.net/woshisap/article/details/7024482 1:getCurrentSession会把Session和当前的线程关联起来, ...
- openSession()与getCurrentSession()的区别
getCurrentSession创建的session会和绑定到当前线程,而openSession不会. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSes ...
- openSession() 与 getCurrentSession() 有何不同和关联呢?
在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在getCurrentSession() 被调用的时候,实际被执 ...
随机推荐
- Wpf 鼠标拖动元素实例
1.Wpf中鼠标捕获和释放 //以矩形为例 //创建鼠标捕获 Mouse.Capture(rectOne); //释放鼠标捕获 rectOne.ReleaseMouseCapture(); 2.Wpf ...
- asp.net中ashx文件如何调用session
如果你要保证数据的安全性,你可以在ashx中使用session验证.如:你的index.aspx中使用jquery回调ashx数据,那么在index.aspx page_load时session[&q ...
- Oracle数据导入导出imp/exp命令总结
racle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中. 利用 ...
- 【转】iOS8 推送 获取 devicetoken
标签:推送 push ios8 devicetoken token xcode6 原文:http://roybaby.blog.51cto.com/1508945/1557854 打开AppDeleg ...
- 解决Undefined symbols for architecture x86_64: 报错 和 ld: warning: ld: warning: ignoring file警告
出现这种错误的情况: 用iphone5模拟器编译程序正常, 用iphone5s以上的模拟器编译出现Undefined symbols for architecture x86_64: 报错 和 ld: ...
- 根据CreateDirectory递归创建多级目录
分为MFC下的和非MFC下的两种,MFC路径是CString类型的,非MFC的路径是wstring类型的. 下面是MFC下的创建目录: void __fastcall RecursiveDirecto ...
- python split()黑魔法
split()用法: #!/usr/bin/python str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.spli ...
- 单选按钮 点击value值自动把单选按钮选中
HTML 代码 <tr> <td align="right">性别:</td> <td><inputt ...
- 更换Python默认软件镜像源
限于一些众所周知的原因,在我们pip安装软件的时候出现类似报错: data = self.read(amt=amt, decode_content=decode_content) File " ...
- openerp 中如何方便对搜索时间段
以前为了方便的搜索时间区间,经常用wizard对方式,设置开始 结束时间,需要做大量对代码工作, 今天看了search view对组成, 可以用2个filter_domain 来做, 这样用户需要输 ...