9.Hibernate OpenSession&getCurrentSession的比较

在比较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 就是:如果有已经使用的,用旧的,如果没有,建新的。

注意:

  1.在实际开发中,往往使用getCurrentSession多,因为一般是处理同一个事务(即是使用一个数据库的情况),所以在一般情况下比较少使用openSession比较老旧的一套接口了。
  2.getCurrentSession将Session保存到ThreadLocal类(线程局部变量),当使用getCurrentSession()的时候,该方法会自动设置或读取Session。

对于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;
 }
}

两篇文章介绍了ThreadLocal

http://lavasoft.blog.51cto.com/62575/51926/
http://ifeve.com/java-threadlocal%E7%9A%84%E4%BD%BF%E7%94%A8/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要添加org.hibernate程序包,你需要执行以下步骤: 1. 下载Hibernate 2. 将Hibernate的jar文件添加到你的项目中 3. 在你的代码中导入所需的类 4. 配置Hibernate的配置文件 5. 创建Hibernate的会话工厂 6. 使用Hibernate进行数据库操作 以下是一个简单的示例,演示如何使用Hibernate: 1. 下载Hibernate并将其解压缩到你的项目目录中。 2. 在你的项目中创建一个lib文件夹,并将Hibernate的jar文件复制到该文件夹中。 3. 在你的代码中导入所需的类,例如: import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; 4. 创建Hibernate的配置文件,例如: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> </session-factory> </hibernate-configuration> 5. 创建Hibernate的会话工厂,例如: Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); 6. 使用Hibernate进行数据库操作,例如: Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); //执行数据库操作 transaction.commit(); session.close(); 以上是一个简单的示例,演示如何使用Hibernate。你可以根据你的项目需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值