您的位置:首页 > 其它

hibernate教程--事务处理详解

2017-04-19 21:19 260 查看

 Hibernate的事务处理

1、事务:

事务就是逻辑上的一组操作,要么全都成功,要么全都失败!!!

2、事务特性:

* 原子性:事务一组操作不可分割.

* 一致性:事务的执行前后,数据完整性要保持一致.

* 隔离性:一个事务在执行的过程中不应该受到其他事务的干扰.

* 持久性:一旦事务结束,数据就永久保存数据库.

 

如果不考虑事务的隔离性引发一些安全性问题:

5大类问题:3类读问题 2类写问题.

3、读问题:

* 脏读:一个事务读到另一个事务未提交数据.

* 不可重复读:一个事务读到另一个事务已经提交数据(update),导致查询结果不一致.

* 虚读:一个事务读到另一个事务已经提交的数据(insert),导致查询结果不一致

 

* 避免三种读的问题:

* 设置事务的隔离级别:

* 未提交读:以上三种读问题 都有可能发生.

* 已提交读:避免脏读,但是不可重复读和虚读有可能发生.

* 重复读:避免脏读和不可重复读,但是虚读是有可能发生.

* 串行的:可以避免以上三种读问题.

 

* 在Hibernate中设置事务的隔离级别:

* 在核心配置文件中:

<property name="hibernate.connection.isolation">4</property>

 

4、 写问题:丢失更新

* 解决;

悲观锁:

乐观锁;

 线程绑定的session:

在Hibernate.cfg.xml中配置一个:

<property name="hibernate.current_session_context_class">thread</property>

* 使用SessionFactory中的getCurrentSession();方法.

* 底层就是ThreadLocal.

 

 5、测试

package com.sihai.hibernate3.test;

import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.sihai.hibernate3.demo1.Customer;
import com.sihai.utils.HibernateUtils;

/**
* Hibernate的事务与并发的测试类:
* @author sihai
*
*/
public class HibernateTest5 {
@Test
public void demo9(){
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();

Customer customer = new Customer();
customer.setCname("李四");
session.save(customer);

tx.commit();
// session.close();
}

@Test
// 本地Session
public void demo8(){
Session session1 = HibernateUtils.getCurrentSession();
Session session2 = HibernateUtils.getCurrentSession();

System.out.println(session1 == session2);
}

@Test
// 本地Session
public void demo7(){
Session session1 = HibernateUtils.openSession();
Session session2 = HibernateUtils.openSession();

System.out.println(session1 == session2);
}

@Test
// 乐观锁解决丢失更新:
public void demo6(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();

Customer customer = (Customer) session.get(Customer.class,3);
customer.setAge(36);

tx.commit();
session.close();
}

@Test
// 乐观锁解决丢失更新:
public void demo5(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();

Customer customer = (Customer) session.get(Customer.class,3);
customer.setCname("铁男");

tx.commit();
session.close();
}

@SuppressWarnings("deprecation")
@Test
// 悲观锁解决丢失更新:
public void demo4(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();

Customer customer = (Customer) session.get(Customer.class,3,LockMode.UPGRADE);
customer.setAge(36);

tx.commit();
session.close();
}

@SuppressWarnings("deprecation")
@Test
// 悲观锁解决丢失更新:
public void demo3(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();

Customer customer = (Customer) session.get(Customer.class,3,LockMode.UPGRADE);
customer.setCname("铁男");

tx.commit();
session.close();
}

@Test
// 演示丢失更新
public void demo2(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();

Customer customer = (Customer) session.get(Customer.class, 3);
customer.setAge(36);

tx.commit();
session.close();
}

@Test
// 演示丢失更新
public void demo1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();

Customer customer = (Customer) session.get(Customer.class, 3);
customer.setCname("铁男");

tx.commit();
session.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: