在比较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. My-sql #1045 - Access denied for user 'root'@'localhost' (using password: NO)

    当你重装数据库后出现这个问题的时候,不要着急,首先你要去你的确定你的数据库已经成功的把服务开启了, 然后确定你的密码和账户,IP都确认的情况下, 去寻找config.inc.php 这个文件,根据配置 ...

  2. C#超时处理(转载)

    /// <summary>    /// 超时处理    ///    ///    /// </summary>    public class TimeoutChecker ...

  3. Orace数据库锁表的处理与总结<摘抄与总结三>

    当Oracle数据库发生TX锁等待时,如果不及时处理常常会引起Oracle数据库挂起,或导致死锁的发生,产生ORA-60的错误. TX锁等待的分析 Oracle数据库中一般使用行级锁. 当Oracle ...

  4. Deep Learning 学习随记(六)Linear Decoder 线性解码

    线性解码器(Linear Decoder) 前面第一章提到稀疏自编码器(http://www.cnblogs.com/bzjia-blog/p/SparseAutoencoder.html)的三层网络 ...

  5. iOS-NSSDate的使用

    取当前时间的秒数 NSTimeInterval time = [[NSDate date] timeIntervalSince1970];long long int date = (long long ...

  6. javascript社交平台分享-新浪微博、QQ微博、QQ好友、QQ空间、人人网

    整理的五个社交平台的分享 <!doctype html> <html lang="en"> <head> <meta charset=&q ...

  7. 【POJ2985】【Treap + 并查集】The k-th Largest Group

    Description Newman likes playing with cats. He possesses lots of cats in his home. Because the numbe ...

  8. ExtJs中动态加载机制研究(转)

    觉得写的太好了,怕弄丢了,转一下:http://extjs.org.cn/node/659 昨天我们team对于extjs的动态加载机制做了些深入研究,这里先share下controller加载的结果 ...

  9. windows8.1 App中webView 使用定位

    windows8.1的webview的网页中没有办法直接定位  要想定位比较费劲   查了好久才发现一个可行的办法 那就是通过后台代码获取位置信息 然后调用页面中已有的获取位置信息的JS方法 把位置信 ...

  10. Python开发【第一章】:Python简介和入门

    Python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC 语言的一种继承. ...