在比较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