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
    

      

  • 相关阅读:
    模式使用详解 手拉手就是职责链吗?
    简单说两句味道
    胡说两句...
    补充说明: 表驱动, 链表与职责链
    C++/CLI和TMP: enum hack相當於static const int吗?
    WCF NetTcpBinding Transport安全模式(9) ClientCredentialType证书验证模式Custom验证模式
    WCF NetTcpBinding Transport安全模式(8) ClientCredentialType证书验证模式 PeerOrChainTrust验证模式
    WCF NetTcpBinding Transport安全模式(2) 默认安全配置
    WCF NetTcpBinding Transport安全模式(7) ClientCredentialType证书验证模式 ChainTrust验证模式
    WCF NetTcpBinding Transport安全模式(6) ClientCredentialType证书验证模式 PeerTrust验证模式
  • 原文地址:https://www.cnblogs.com/qf123/p/10174177.html
Copyright © 2011-2022 走看看