javaEE Hibernate, 事务, 事务隔离级别配置, getCurrentSession()


Test.java:

package cn.xxx.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import cn.xxx.domain.Customer;
import cn.xxx.utils.HibernateUtils;

//测试getCurrentSession
public class Demo {

	@Test
	//返回同一个与线程绑定的session
	public void fun1(){
		Session session1 = HibernateUtils.getCurrentSession();  // session.getCurrentSession()。不需要手动关闭该session;提交事务时,会自动关闭。
		Session session2 = HibernateUtils.getCurrentSession();  // 需要在hibernate.cfg.xml配置文件中配置session与当前线程绑定
		
		System.out.println(session1==session2); //true 同一个线程获取的session是相同的,可以用以控制事务。
	}
	
	@Test
	//返回不同的session
	public void fun2(){
		Session session1 = HibernateUtils.openSession();  // session.openSession()。 获取一个新的session
		Session session2 = HibernateUtils.openSession();
		
		System.out.println(session1==session2); //false
	}
	
}
HibernateUtils.java(Hibernate连接数据库的工具类):
package cn.xxx.utils;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class HibernateUtils {
	private static SessionFactory sf;  // 一个web中只需要一个SessionFactory对象。
	
	static{
		//1 创建Configuration对象,默认加载src下的hibernate.cfg.xml配置文件
		Configuration conf = new Configuration().configure();
		//2 根据配置信息,创建 SessionFactory对象
		 sf = conf.buildSessionFactory();
	}
	
	//获得session => 获得全新session
	public static Session openSession(){
		//3 获得session
		Session session = sf.openSession();
		return session;
	}
	
	//获得session => 获得与线程绑定的session(当前session)
	public static Session getCurrentSession(){
		//3 获得session
		Session session = sf.getCurrentSession(); // 需要在主配置文件中配置session与当前线程绑定。提交事务时自动关闭session,不需要手动关闭。
		
		return session;
	}
}
 
hibernate.cfg.xml(主配置文件,事务隔离级别配置,配置session与当前线程绑定(控制事务)):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//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:///数据库名</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- 可选配置 -->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>

		 <!-- 指定hibernate操作数据库时的隔离级别 
			#hibernate.connection.isolation 1|2|4|8		
			0001	1	读未提交 (三种并行问题都不能解决,效率最高)
			0010	2	读已提交 (解决脏读)(Oracle默认)
			0100	4	可重复读 (解决脏读、不可重复读)(MySql默认)
			1000	8	串行化   (解决脏读、不可重复读、幻读) (效率最低)
		 -->
		<property name="hibernate.connection.isolation">4</property>
		<!-- 指定session与当前线程绑定。同一个线程用相同的session来控制事务 -->
		<property name="hibernate.current_session_context_class">thread</property>
		 
		<mapping resource="cn/itheima/domain/Customer.hbm.xml" />
		
	</session-factory>
</hibernate-configuration>


猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/81054162