Hibernate乐观锁兑现之Timestamp

Hibernate乐观锁实现之Timestamp

Hibernate乐观锁实现之Timestamp

通过在表中及POJO中增加一个Timestamp字段来表示记录的最后更新时间,来达到多用户同时更改一条数据的冲突,这个timestamp由数据库自动添加,无需人工干预

数据库结构:

 

Hibernate乐观锁兑现之Timestampcreate table studentTimestamp(id varchar(32),name varchar(32),lastUpdateDateTime timestamp not null default current_timestamp on update current_timestamp);
Hibernate乐观锁兑现之Timestamp

 

pojos:

 

Hibernate乐观锁兑现之Timestamppackage Timestamp;
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
import java.util.Date;
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
public class Student {
Hibernate乐观锁兑现之Timestamp  
private String id;
Hibernate乐观锁兑现之Timestamp  
private String name;
Hibernate乐观锁兑现之Timestamp  
private Date lastUpdateDateTime;
Hibernate乐观锁兑现之Timestamp
public String getId() {
Hibernate乐观锁兑现之Timestamp    
return id;
Hibernate乐观锁兑现之Timestamp}

Hibernate乐观锁兑现之Timestamp
public Date getLastUpdateDateTime() {
Hibernate乐观锁兑现之Timestamp    
return lastUpdateDateTime;
Hibernate乐观锁兑现之Timestamp}

Hibernate乐观锁兑现之Timestamp
public void setLastUpdateDateTime(Date lastUpdateDateTime) {
Hibernate乐观锁兑现之Timestamp    
this.lastUpdateDateTime = lastUpdateDateTime;
Hibernate乐观锁兑现之Timestamp}

Hibernate乐观锁兑现之Timestamp
public void setId(String id) {
Hibernate乐观锁兑现之Timestamp    
this.id = id;
Hibernate乐观锁兑现之Timestamp}

Hibernate乐观锁兑现之Timestamp
public String getName() {
Hibernate乐观锁兑现之Timestamp    
return name;
Hibernate乐观锁兑现之Timestamp}

Hibernate乐观锁兑现之Timestamp
public void setName(String name) {
Hibernate乐观锁兑现之Timestamp    
this.name = name;
Hibernate乐观锁兑现之Timestamp}

Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp}

Hibernate乐观锁兑现之Timestamp

 Student.hbm.xml

 

Hibernate乐观锁兑现之Timestamp<?xml version="1.0" encoding="utf-8"?>
Hibernate乐观锁兑现之Timestamp
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
Hibernate乐观锁兑现之Timestamp"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
Hibernate乐观锁兑现之Timestamp
<!-- 
Hibernate乐观锁兑现之Timestamp    Mapping file autogenerated by MyEclipse - Hibernate Tools
Hibernate乐观锁兑现之Timestamp
-->
Hibernate乐观锁兑现之Timestamp
<hibernate-mapping>
Hibernate乐观锁兑现之Timestamp
<class name="Timestamp.Student" table="studentTimestamp" >
Hibernate乐观锁兑现之Timestamp    
<id name="id" unsaved-value="null">
Hibernate乐观锁兑现之Timestamp      
<generator class="uuid.hex"></generator>
Hibernate乐观锁兑现之Timestamp    
</id>
Hibernate乐观锁兑现之Timestamp    
<!-- timestamp标签必须跟在id标签后面 -->
Hibernate乐观锁兑现之Timestamp    
<timestamp name="lastUpdateDateTime" column="lastUpdateDateTime"></timestamp>
Hibernate乐观锁兑现之Timestamp    
<property name="name" type="string" column="name"></property>  
Hibernate乐观锁兑现之Timestamp
</class>
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
</hibernate-mapping>
Hibernate乐观锁兑现之Timestamp

 

Hibernate.cfg.xml

 

Hibernate乐观锁兑现之Timestamp<?xml version='1.0' encoding='UTF-8'?>
Hibernate乐观锁兑现之Timestamp
<!DOCTYPE hibernate-configuration PUBLIC
Hibernate乐观锁兑现之Timestamp          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
Hibernate乐观锁兑现之Timestamp          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
<!-- Generated by MyEclipse Hibernate Tools.                   -->
Hibernate乐观锁兑现之Timestamp
<hibernate-configuration>
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
<session-factory>
Hibernate乐观锁兑现之Timestamp    
<property name="connection.username">root</property>
Hibernate乐观锁兑现之Timestamp    
<property name="connection.url">
Hibernate乐观锁兑现之Timestamp        jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312
&amp;useUnicode=true
Hibernate乐观锁兑现之Timestamp    
</property>
Hibernate乐观锁兑现之Timestamp    
<property name="dialect">
Hibernate乐观锁兑现之Timestamp        org.hibernate.dialect.MySQLDialect
Hibernate乐观锁兑现之Timestamp    
</property>
Hibernate乐观锁兑现之Timestamp    
<property name="myeclipse.connection.profile">mysql</property>
Hibernate乐观锁兑现之Timestamp    
<property name="connection.password">1234</property>
Hibernate乐观锁兑现之Timestamp    
<property name="connection.driver_class">
Hibernate乐观锁兑现之Timestamp        com.mysql.jdbc.Driver
Hibernate乐观锁兑现之Timestamp    
</property>
Hibernate乐观锁兑现之Timestamp    
<property name="hibernate.dialect">
Hibernate乐观锁兑现之Timestamp        org.hibernate.dialect.MySQLDialect
Hibernate乐观锁兑现之Timestamp    
</property>
Hibernate乐观锁兑现之Timestamp    
<property name="hibernate.show_sql">true</property>
Hibernate乐观锁兑现之Timestamp    
<property name="current_session_context_class">thread</property>
Hibernate乐观锁兑现之Timestamp    
<property name="jdbc.batch_size">15</property>
Hibernate乐观锁兑现之Timestamp    
<mapping resource="Timestamp/Student.hbm.xml" />
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
</session-factory>
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
</hibernate-configuration>

 

测试代码:

 

Hibernate乐观锁兑现之Timestamppackage Timestamp;
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
import java.io.File;
Hibernate乐观锁兑现之Timestamp
import java.util.Iterator;
Hibernate乐观锁兑现之Timestamp
import java.util.Set;
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
import org.hibernate.Session;
Hibernate乐观锁兑现之Timestamp
import org.hibernate.SessionFactory;
Hibernate乐观锁兑现之Timestamp
import org.hibernate.Transaction;
Hibernate乐观锁兑现之Timestamp
import org.hibernate.cfg.Configuration;
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
public class Test {
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp    
public static void main(String[] args) {
Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp        String filePath
=System.getProperty("user.dir")+File.separator+"src/Timestamp"+File.separator+"hibernate.cfg.xml";
Hibernate乐观锁兑现之Timestamp        File file
=new File(filePath);
Hibernate乐观锁兑现之Timestamp        System.out.println(filePath);
Hibernate乐观锁兑现之Timestamp        SessionFactory sessionFactory
=new Configuration().configure(file).buildSessionFactory();
Hibernate乐观锁兑现之Timestamp        Session session
=sessionFactory.openSession();
Hibernate乐观锁兑现之Timestamp        Transaction t
=session.beginTransaction();
Hibernate乐观锁兑现之Timestamp        
Hibernate乐观锁兑现之Timestamp        Student stu
=new Student();
Hibernate乐观锁兑现之Timestamp        stu.setName(
"tom11");
Hibernate乐观锁兑现之Timestamp        session.save(stu);
Hibernate乐观锁兑现之Timestamp        t.commit();
Hibernate乐观锁兑现之Timestamp      
Hibernate乐观锁兑现之Timestamp        
Hibernate乐观锁兑现之Timestamp        
/*
Hibernate乐观锁兑现之Timestamp         * 模拟多个session操作student数据表
Hibernate乐观锁兑现之Timestamp         
*/

Hibernate乐观锁兑现之Timestamp        
Hibernate乐观锁兑现之Timestamp        Session session1
=sessionFactory.openSession();
Hibernate乐观锁兑现之Timestamp        Session session2
=sessionFactory.openSession();
Hibernate乐观锁兑现之Timestamp        Student stu1
=(Student)session1.createQuery("from Student s where s.name='tom11'").uniqueResult();
Hibernate乐观锁兑现之Timestamp        Student stu2
=(Student)session2.createQuery("from Student s where s.name='tom11'").uniqueResult();
Hibernate乐观锁兑现之Timestamp        
Hibernate乐观锁兑现之Timestamp        
//这时候,两个版本号是相同的
Hibernate乐观锁兑现之Timestamp
        System.out.println("v1="+stu1.getLastUpdateDateTime()+"--v2="+stu2.getLastUpdateDateTime());
Hibernate乐观锁兑现之Timestamp        
Hibernate乐观锁兑现之Timestamp        Transaction tx1
=session1.beginTransaction();
Hibernate乐观锁兑现之Timestamp        stu1.setName(
"session1");
Hibernate乐观锁兑现之Timestamp        tx1.commit();
Hibernate乐观锁兑现之Timestamp        
//这时候,两个版本号是不同的,其中一个的版本号递增了
Hibernate乐观锁兑现之Timestamp
        System.out.println("v1="+stu1.getLastUpdateDateTime()+"--v2="+stu2.getLastUpdateDateTime());
Hibernate乐观锁兑现之Timestamp        
Hibernate乐观锁兑现之Timestamp        Transaction tx2
=session2.beginTransaction();
Hibernate乐观锁兑现之Timestamp        stu2.setName(
"session2");
Hibernate乐观锁兑现之Timestamp        tx2.commit();
Hibernate乐观锁兑现之Timestamp        
Hibernate乐观锁兑现之Timestamp        
Hibernate乐观锁兑现之Timestamp        
Hibernate乐观锁兑现之Timestamp    }

Hibernate乐观锁兑现之Timestamp
Hibernate乐观锁兑现之Timestamp}

Hibernate乐观锁兑现之Timestamp

 

测试结果:


Hibernate: insert into studentTimestamp (lastUpdateDateTime, name, id) values (?, ?, ?)
Hibernate: select student0_.id as id0_, student0_.lastUpdateDateTime as lastUpda2_0_, student0_.name as name0_ from studentTimestamp student0_ where student0_.name='tom11'

Hibernate: select student0_.id as id0_, student0_.lastUpdateDateTime as lastUpda2_0_, student0_.name as name0_ from studentTimestamp student0_ where student0_.name='tom11'
v1=2007-12-12 16:30:53.0--v2=2007-12-12 16:30:53.0

Hibernate: update studentTimestamp set lastUpdateDateTime=?, name=? where id=? and lastUpdateDateTime=?

Exception in thread "main" org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): 

v1=2007-12-12 16:30:54.844--v2=2007-12-12 16:30:53.0

Hibernate: update studentTimestamp set lastUpdateDateTime=?, name=? where id=? and lastUpdateDateTime=?