七、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

  

转载于:https://www.cnblogs.com/qf123/p/10174177.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值