悲观锁、乐观锁

1、悲观锁(处理并行问题):悲观锁的实现,通常依赖于数据库机制,在整个过程中将数据锁定,其它任何用户都不能读取或修改
//model
public class Inventory {

	private int itemNo;
	
	private String itemName;
	
	private int quantity;

	public int getItemNo() {
		return itemNo;
	}

	public void setItemNo(int itemNo) {
		this.itemNo = itemNo;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
}
//Inventory.hbm.xml
<hibernate-mapping>
	<class name="com.bjsxt.hibernate.Inventory" table="t_inventory">
		<id name="itemNo">
			<generator class="native"/>
		</id>
		<property name="itemName" length = "10"/>
		<property name="quantity"/>
	</class>
</hibernate-mapping>
//test
import org.hibernate.LockMode;
import org.hibernate.Session;

import junit.framework.TestCase;

public class PessimisticLockingTest extends TestCase {// 并行模拟

	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//加载的时候将其锁住,LockMode指定锁模式
			Inventory inv = (Inventory)session.load(Inventory.class, 1, LockMode.UPGRADE);
			System.out.println("itemName=" + inv.getItemName());
			System.out.println("quantity=" + inv.getQuantity());
			inv.setQuantity(inv.getQuantity() - 200);
			
			session.update(inv);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}	
	}
	
	public void testLoad2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Inventory inv = (Inventory)session.load(Inventory.class, 1, LockMode.UPGRADE);
			System.out.println("itemName=" + inv.getItemName());
			System.out.println("quantity=" + inv.getQuantity());
			inv.setQuantity(inv.getQuantity() - 200);
			
			session.update(inv);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}	
	}
}

2、乐观锁:只能检查,不能防止(处理并发问题)

        大多数基于数据版本记录机制(version)实现,一般是在数据库表中加入一个version字段读取数据时将版本号一同读出,之后更新数据时版本号加一,如果提交数据时版本号小于或等于数据表中的版本号,则认为数据是过期的,否则给予更新
//model
public class Inventory {

	private int itemNo;
	
	private String itemName;
	
	private int quantity;
	
	private int version;

	public int getItemNo() {
		return itemNo;
	}

	public void setItemNo(int itemNo) {
		this.itemNo = itemNo;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}
}
//Inventory.hbm.xml
<hibernate-mapping>                                 <!-- optimistic-lock指定乐观锁模式 -->
	<class name="com.bjsxt.hibernate.Inventory" table="t_inventory" optimistic-lock="version">
		<id name="itemNo">
			<generator class="native"/>
		</id>
		<version name="version"/><!-- version指定辨别字段 -->
		<property name="itemName" length = "10"/>
		<property name="quantity"/>
	</class>
</hibernate-mapping>
//test
import org.hibernate.Session;

public class InitData {

	public static void main(String[] args) {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Inventory inv = new  Inventory();
			inv.setItemNo(1001);
			inv.setItemName("脑白金");
			inv.setQuantity(1000);
			
			session.save(inv);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}	
	}
}



import org.hibernate.LockMode;
import org.hibernate.Session;

import junit.framework.TestCase;

public class OptimisticLockingTest extends TestCase {

	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Inventory inv = (Inventory)session.load(Inventory.class, 1);
			System.out.println("itemName=" + inv.getItemName());
			System.out.println("version=" + inv.getVersion());
			System.out.println("quantity=" + inv.getQuantity());
			inv.setQuantity(inv.getQuantity() - 200);
			session.update(inv);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}	
	}
	
	public void testLoad2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Inventory inv = (Inventory)session.load(Inventory.class, 1);
			System.out.println("itemName=" + inv.getItemName());
			System.out.println("version=" + inv.getVersion());
			System.out.println("quantity=" + inv.getQuantity());
			inv.setQuantity(inv.getQuantity() - 200);
			session.update(inv);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}	
	}
}

你可能感兴趣的:(Hibernate,xml,JUnit)