zoukankan      html  css  js  c++  java
  • 七、hibernate的事务使用

    hibernate中事务隔离级别

    • 1:读未提交
    • 2:读已提交
    • 4:可重复读
    • 8:可串行化

    hibernate事务使用

    1. 在核心配置文件中配置事务隔离级别
      1. <property name="hibernate.connection.isolation">4</property>
    2. 确保一个逻辑事务中的session是同一个
      1. 核心配置文件中配置事务当前线程绑定session:<property name="hibernate.current_session_context_class" >thread</property>
      2. 通过sessionFactory.getCurrentSession()来获取session(getCurrentSession()方法默认不开启,必须配置事务当前线程绑定session来开启)
    3. 注:使用getCurrentSession()方法获取的session操作完不需要关闭,线程结束会自动关闭

    核心配置文件hibernate.cfg.xml

    <hibernate-configuration>
    	<session-factory>
    		<property name="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
    		<property name="hibernate.connection.url" >jdbc:mysql:///test02</property>
    		<property name="hibernate.connection.username" >root</property>
    		<property name="hibernate.connection.password" >root</property>
    		<property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property>
    		
    		<property name="hibernate.hbm2ddl.auto" >create</property>
    		<property name="hibernate.show_sql" >true</property>
    		<!-- <property name="hibernate.format_sql" >true</property> -->
    		
    		<property name="hibernate.connection.isolation">4</property>
    		<property name="hibernate.current_session_context_class" >thread</property>
    		
    		<mapping resource="com/qf/entity/Teacher.hbm.xml"/>
    		<mapping resource="com/qf/entity/Student.hbm.xml"/>
    	</session-factory>
    </hibernate-configuration>

    sessionFactory.getCurrentSession()方法的实现

    内部使用了ThreadLocal来实现线程绑定session

    public Session getCurrentSession() throws HibernateException {
    	if ( currentSessionContext == null ) {
    		throw new HibernateException( "No CurrentSessionContext configured!" );
    	}
    	return currentSessionContext.currentSession();
    } 
    @Override
    public final Session currentSession() throws HibernateException {
    	Session current = existingSession( factory() );
    	if ( current == null ) {
    		current = buildOrObtainSession();
    		// register a cleanup sync
    		current.getTransaction().registerSynchronization( buildCleanupSynch() );
    		// wrap the session in the transaction-protection proxy
    		if ( needsWrapping( current ) ) {
    			current = wrap( current );
    		}
    		// then bind it
    		doBind( current, factory() );
    	}
    	else {
    		validateExistingSession( current );
    	}
    	return current;
    }

    1. 测试getCurrentSession()方法获取的session操作完是否会自己关闭

    @Test
    public void test() {
    	Configuration cfg = new Configuration().configure();
    	SessionFactory factory = cfg.buildSessionFactory();
    	Session session = factory.getCurrentSession();
    	
    	Transaction tx = session.beginTransaction();
    	
    	Student student = session.get(Student.class, 1L);
    	System.out.println(student);
    	tx.commit();
    	session.close();
    }

    -------------------------------------Junit-----------------------------------------

    org.hibernate.SessionException: Session was already closed

    线程结束,session已经自己关闭了,不需要再去手动关闭

    2. 测试两次通过getCurrentSession()方法获取的session是否一致

    @Test
    public void test() {
    	Configuration cfg = new Configuration().configure();
    	SessionFactory factory = cfg.buildSessionFactory();
    	
    	Session session1 = factory.getCurrentSession();
    	Session session2 = factory.getCurrentSession();
    	System.out.println("两次通过getCurrentSession()获取的session是否一致:"+(session1==session2));
    	
    	Session session3 = factory.openSession();
    	Session session4 = factory.openSession();
    	System.out.println("两次通过openSession()获取的session是否一致:"+(session3==session4));
    } 

    -------------------------------------console-----------------------------------------

    两次通过getCurrentSession()获取的session是否一致:true
    两次通过openSession()获取的session是否一致:false
    

      

  • 相关阅读:
    asp.net mvc 项目架构解析
    新手如何发网站外链,网站的外链如何发,发外链的方法集合
    win2008r2的iis7.5手动建站方法,iis7.5中用独立用户建立网站的方法,提高网站安全性
    ex:Could not load file or assembly 'System.Web.Helpers, Version=2.0.0.0, Culture=neutral, . 系统找不到指定的文件。
    微信开放平台---网站应用开发---微信登录功能 简介
    如何使用ILSpy 把发布版本反编译成源码
    沐雪多用户微信公众平台开发源码,商城小程序源码(2018年最新的asp.net C# 微信源码,小程序源码)
    2015淘宝最新排名新规则
    群主微信sdk说明地址
    Quantization Method
  • 原文地址:https://www.cnblogs.com/qf123/p/10174177.html
Copyright © 2011-2022 走看看