openSession和getCurrentSession的比较

在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法。

在进行配置信息管理时,我们一般进行一下简单步骤:

1  Configuration cfg = new Configuration();  // 获得配置信息对象
2 SessionFactory sf = cfg.configure().buildSessionFactory(); //解析并建立Session工厂
3
4 1. Session session = sf.getCurrentSession(); // 获得Session
5 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方式 :


 1 import org.hibernate.Session;
2 import org.hibernate.SessionFactory;
3 import org.hibernate.cfg.Configuration;
4 import com.hibernate.model.Student; // 注意包路径
5
6 public class StudentTest {
7 public static void main(String[] args) {
8 Student s = new Student();
9 s.setId(1);
10 s.setName("s1");
11 s.setAge(1);
12
13 Configuration cfg = new Configuration(); // 获得配置信息对象
14 SessionFactory sf = cfg.configure().buildSessionFactory(); //解析并建立Session工厂
15 Session session = sessionFactory.openSession(); // 打开Session
16
17 session.beginTransaction(); // 看成一个事务,进行操作
18 session.save(s); // 会找到 Student 这个类,寻找set方法
19 session.getTransaction().commit(); // 提交对数据的操作
20 session.close();
21 sf.close();
22 }
23 }

2.getCurrentSession方式 :


 1 import org.hibernate.Session;
2 import org.hibernate.SessionFactory;
3 import org.hibernate.cfg.Configuration;
4 import com.hibernate.model.Student; // 注意包路径
5
6 public class StudentTest {
7 public static void main(String[] args) {
8
9 Student s = new Student();
10 s.setId(1);
11 s.setName("s1");
12 s.setAge(1);
13
14 Configuration cfg = new Configuration(); // 获得配置信息对象
15 SessionFactory sf = cfg.configure().buildSessionFactory(); //解析并建立Session工厂
16 Session session = sessionFactory.getCurrentSession(); // 打开Session
17
18 session.beginTransaction(); // 看成一个事务,进行操作
19 session.save(s); // 会找到 Student 这个类,寻找set方法
20 session.getTransaction().commit(); // 提交对数据的操作
21
22 sf.close();
23 }
24 }

Student 类代码 :


 1 package com.hibernate.model;
2 public class Student {
3 private int id;
4 private String name;
5 private int age;
6 public int getId() {
7 return id;
8 }
9 public void setId(int id) {
10 this.id = id;
11 }
12 public String getName() {
13 return name;
14 }
15 public void setName(String name) {
16 this.name = name;
17 }
18 public int getAge() {
19 return age;
20 }
21 public void setAge(int age) {
22 this.age = age;
23 }
24 }

openSession和getCurrentSession的比较,布布扣,bubuko.com

时间: 2024-08-24 07:13:23

openSession和getCurrentSession的比较的相关文章

SessionFactory的openSession与getCurrentSession区别

SessionFactory 1 用来产生和管理sesssion 2 通常情况下,每个应用只需要一个SessionFactory,除非要访问多个数据库的情况 3 openSession()与openSession() (1) openSession()总是创建新的session,需要手动close(). (2)getCurrentSession()事务提交则自动关闭.从上下文环境中获得session,如果当时环境中不存就创建新的.如果环境中存在就使用环境中的,而且每次得到的都是同一个sessio

由openSession、getCurrentSession和HibernateDaoSupport浅谈Spring对事物的支持

由openSession.getCurrentSession和HibernateDaoSupport浅谈Spring对事物的支持 Spring和Hibernate的集成的一个要点就是对事务的支持,openSession.getCurrentSession都是编程式事务(手动设置事务的提交.回滚)中重要的对象,HibernateDaoSupport则提供了更方便的声明式事务支持. Hibernate中最重要的就是Session对象的引入,它是对jdbc的深度封装,包括对事务的处理,Session对

4.openSession() 、 getCurrentSession()与session上下文

openSession()每次都打开一个新的session,用了openSession(),要记得close()掉 getCurrentSession()在session上下文(hibernate配置文件中的current_session_context_class有设定)找session,如果有,则返回该session,否则会新建一个session.不需要调用close(),transaction commit后会自动关闭.openSession()与getCurrentSession()不能

HIbernate中openSession和getCurrentSession

??这两者的区别网上很多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题. ??openSession和getCurrentSession的根本区别在于有没有绑定当前线程,所以,使用方法有差异: * openSession没有绑定当前线程,所以,使用完后必须关闭, * currentSession和当前线程绑定,在事务结束后会自动关闭. ??今天在复习Hibernate时,看到Hibernate检索方式的时候,写了一个小例子: @Test public void query01()

2014年12月8日-configuration类以及openSession和getCurrentSession的区别

Hibernate的configuration类: configuration类是用来加载hibernate配置文件的,默认的是读取hibernate.cfg.xml配置文件的信息. Configuration cfg = new Configuration().configure(); //Configuration cfg = new AnnotationConfiguration().configure(); //使用annotation的加载配置文件的方法 SessionFactory

java openSession和getCurrentSession的比较

比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = new Configuration();  // 获得配置信息对象 SessionFactory sf = cfg.configure().buildSessionFactory(); //解析并建立Session工厂 1. Session session = sf.getCurrentSession(

hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别

1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭 这里getCurrentSession本地事务(本地事务:jdbc)时 要在配置文件里进行如下设置 * 如果使用的是本地事务(jdbc事务) <property name="hibernate.current_session_context_class"

getCurrentSession 与 openSession() 的区别&lt;转&gt;

1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭 这里getCurrentSession本地事务(本地事务:jdbc)时 要在配置文件里进行如下设置 * 如果使用的是本地事务(jdbc事务) <property name="hibernate.current_session_context_class"

Hibernate对象的CRUD操作

1.  Hibernate对象的CRUD操作 1.1.  对象的三种状态 瞬时(Transient) - 由new操作符创建,且尚未与HibernateSession 关联的对象被认定为瞬时(Transient)的.瞬时(Transient)对象不会被持久化到数据库中,也不会被赋予持久化标识(identifier).如果瞬时(Transient)对象在程序中没有被引用,它会被垃圾回收器(garbage collector)销毁.使用Hibernate Session可以将其变为持久(Persis