在比较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. 层叠样式优先级CSS

    按照W3School网站(点这里直达)的说法,当同一个 HTML 元素被不止一个样式定义时,它们是有优先级之分的,如下,将优先级从小到大排列出来,其中4的优先级最高: 1.浏览器缺省设置2.外部样式表 ...

  2. MySQL数据库迁移详细步骤(转)

    ========================================================================================== 一.背景简介 == ...

  3. sublime text snippet代码片断

    $0 代表补全代码后放的位置   0 的权重是最低的 $1 最高 也等于${1:}  ${1: name}    1输入点的序号(1权重最高) name 自动补全的默认值      <conte ...

  4. SGU 111.Very simple problem

    题目大意:              求平方不大于n(n<=10^1000)的最大的数. 分析:              二分+高精度乘法 或者 高精度开方...               ...

  5. SGU 163.Wise King

    一道题目长的水题.... 总结就一句话,给出n个(-3~3)的数,一个数m,取任意个数是使这些数的m次幂之和最大. code #include <iostream> #include &l ...

  6. POJ 2112.Optimal Milking (最大流)

    时间限制:2s 空间限制:30M 题意: 有K台挤奶机(编号1~K),C头奶牛(编号K+1~K+C),给出各点之间距离.现在要让C头奶牛到挤奶机去挤奶,每台挤奶机只能处理M头奶牛,求使所走路程最远的奶 ...

  7. java中jdk环境配置

    配置java环境,俗称jdk环境 首先进入配置环境的目录下:右键鼠标我的电脑->属性->高级系统设置->环境变量,在对应的"系统变量"框下配置一下变量: 规范的配 ...

  8. DEDECMS使用SQL命令批量替换语句

    1.更改文章中的内容 update dede_addonarticle set body=replace(body,'原来的字符','替换后的字符') 2,替换文章标题 update dede_arc ...

  9. javascript获取div高度

    DOM的: 获得了div的高度值 = document.getElementById("目标id").offsetHeight;

  10. Aspnet MVC 异步调用

    一个简图来描述下Aspnet MVC下的异步调用 { request } / \/ -------ISS------- > work thread | \ | \ route - aysn co ...