在比较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;
}
}
相关推荐
<property name="driverClass" value="${driverClass}"></property> <property name="user" value="${username}"></property> <property name="password" value="${password}"></property> ...
博文链接:https://shaqiang32.iteye.com/blog/201918
NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085
private static final ThreadLocal<Session> sessionThread=new ThreadLocal<Session>(); private static final ThreadLocal<Transaction> transactionThread=new ThreadLocal<Transaction>(); private static ...
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/itcast0807crm</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> ...
<property name="connection.url">jdbc:mysql://127.0.0.1/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">1234</property> <!-- SQL ...
查询出列表,也就是返回list, 在我们这个例子中也就是 List<User> , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory ...
List<User> list=query.list(); if(list.size()>0){ flag=true; } return flag; } public List<User> selectAll() { System.out.println("dao中查询所有"); Session session=sessionFactory....
hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate
9、openSession和getCurrentSession 90 10、拦截器的作用?拦截器和过滤器的区别? 91 11、struts.xml中result的type有哪些类型? 91 12、什么时候用JDBC什么时候用Hibernete; 91 13、hibernate 数据的三个状态 91 ...
123.在 hibernate 中 getCurrentSession 和 openSession 的区别是什么? 124.hibernate 实体类必须要有无参构造函数吗?为什么? 十三、Mybatis 125.mybatis 中 #{}和 ${}的区别是什么? 126.mybatis 有几种分页方式...
1)增加代码自动保存功能,在代码生成物中含有sgs文件夹,内有以 <项目名>_original.sgs命名的生成生成物的SGS源码。 2)增强SGS编译器,使其支持call magic魔法语句,自动生成棱柱列表,便利开发。 3)增强修复S2SH...
NHibernate中的Session示例源代码,适合对NHibernate的使用有一定的了解,并想了解NHibernate源代码及其软件结构的人。
找这个资源的人都知道这是干什么的,就不多说什么了,
hibernate 经典文档,学习hibernate 必备的文档,深入浅出,非常实用,强烈推荐!
2.由hibernate.cfg.xml中的<mapping resource="com/xx/User.hbm.xml"/>读取并解析映射信息 3.通过config.buildSessionFactory();//创建SessionFactory 4.sessionFactory.openSession();//打开Sesssion 5.session....
2.由hibernate.cfg.xml中的<mapping resource="com/xx/User.hbm.xml"/>读取并解析映射信息 3.通过config.buildSessionFactory();//创建SessionFactory 4.sessionFactory.openSession();//打开Sesssion 5.session....