在比较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的比较的更多相关文章

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

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

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

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

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

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

  4. Hibernate之openSession与getCurrentSession的区别

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

  5. HIbernate中openSession和getCurrentSession

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

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

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

  7. Hibernate关于openSession和getCurrentSession的理解

    来源(转载):http://blog.csdn.net/woshisap/article/details/7024482 1:getCurrentSession会把Session和当前的线程关联起来, ...

  8. openSession()与getCurrentSession()的区别

    getCurrentSession创建的session会和绑定到当前线程,而openSession不会. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSes ...

  9. openSession() 与 getCurrentSession() 有何不同和关联呢?

    在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在getCurrentSession() 被调用的时候,实际被执 ...

随机推荐

  1. 学点bootstrap

  2. C#使用Process类调用外部程序(转)

    在程序开发中,一个程序经常需要去调用其他的程序,C#中Process类正好提供了这样的功能.它提供对本地和远程进程的访问并使您能够启动和停止本地系统进程.一.启动进程实例 Process myProc ...

  3. 关于Android4.x系统默认显示方向各种修改

    1.设置属性值 在device.mk文件中加入PRODUCT_PROPERTY_OVERRIDES += \ ro.sf.hwrotation=180 2.设置屏幕默认显示方向 在frameworks ...

  4. angularjs hover

    <ul class="pdl-15"><li ng-repeat="order in vm.selectOrders" ng-class=&q ...

  5. 【SSMS增强工具】SQL Sharper 2014介绍

    产品介绍 SQL Sharper是一款SQL Server Management Studio插件,用于数据库对象快速查询.表结构查询.优化查询结果导出.代码生成等方面. 适用人群:T-SQL开发者. ...

  6. TabControl控件中TabPage的显示和隐藏

    TabPage里面含有方法Hide和Show,但没有任何作用,实际隐藏和显示需要使用如下2个方法 方法一:此方法比较简单 TabPageServo.Parent = Nothing   //隐藏 Ta ...

  7. Bootstrap_表单_表单控件

    一.输入框input 单行输入框,常见的文本输入框,也就是input的type属性值为text. 在Bootstrap中使用input时也必须添加type类型,如果没有指定type类型,将无法得到正确 ...

  8. POJ3274-牛的属性-HASH-ACM

    原题:POJ3274 参考:进击的阿俊 已知有n头牛,用一个K位二进制数Ak,Ak-1,...,A1表示一头牛具有的特征,Ai=1表示具有特征i.现给定按顺序排列的N头牛的k位特征值,称某个连续范围内 ...

  9. Android SurfaceView使用

    与View区别 更新View任务太重会导致UI线程阻塞 而SurfaceView不会,可以在UI线程之外更新UI 工程代码 SurfaceViewDemo.zip ------------------ ...

  10. git extrad_addons 部署说明

    注册一个git账号 : 网址:  https://github.com/ 1:安装git   sudo apt-get install git 2:  b把urc扩展占模块pull下来    cd   ...