Hibernate乐观锁实现之Timestamp
来源:互联网 发布:mysql的运行原理 编辑:程序博客网 时间:2024/05/22 13:14
通过在表中及POJO中增加一个Timestamp字段来表示记录的最后更新时间,来达到多用户同时更改一条数据的冲突,这个timestamp由数据库自动添加,无需人工干预
数据库结构:
pojos:
import java.util.Date;
public class Student ...{
private String id;
private String name;
private Date lastUpdateDateTime;
public String getId() ...{
return id;
}
public Date getLastUpdateDateTime() ...{
return lastUpdateDateTime;
}
public void setLastUpdateDateTime(Date lastUpdateDateTime) ...{
this.lastUpdateDateTime = lastUpdateDateTime;
}
public void setId(String id) ...{
this.id = id;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
}
Student.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="Timestamp.Student" table="studentTimestamp" >
<id name="id" unsaved-value="null">
<generator class="uuid.hex"></generator>
</id>
<!-- timestamp标签必须跟在id标签后面 -->
<timestamp name="lastUpdateDateTime" column="lastUpdateDateTime"></timestamp>
<property name="name" type="string" column="name"></property>
</class>
</hibernate-mapping>
Hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312&useUnicode=true
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="jdbc.batch_size">15</property>
<mapping resource="Timestamp/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
测试代码:
import java.io.File;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test ...{
public static void main(String[] args) ...{
String filePath=System.getProperty("user.dir")+File.separator+"src/Timestamp"+File.separator+"hibernate.cfg.xml";
File file=new File(filePath);
System.out.println(filePath);
SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction t=session.beginTransaction();
Student stu=new Student();
stu.setName("tom11");
session.save(stu);
t.commit();
/**//*
* 模拟多个session操作student数据表
*/
Session session1=sessionFactory.openSession();
Session session2=sessionFactory.openSession();
Student stu1=(Student)session1.createQuery("from Student s where s.name='tom11'").uniqueResult();
Student stu2=(Student)session2.createQuery("from Student s where s.name='tom11'").uniqueResult();
//这时候,两个版本号是相同的
System.out.println("v1="+stu1.getLastUpdateDateTime()+"--v2="+stu2.getLastUpdateDateTime());
Transaction tx1=session1.beginTransaction();
stu1.setName("session1");
tx1.commit();
//这时候,两个版本号是不同的,其中一个的版本号递增了
System.out.println("v1="+stu1.getLastUpdateDateTime()+"--v2="+stu2.getLastUpdateDateTime());
Transaction tx2=session2.beginTransaction();
stu2.setName("session2");
tx2.commit();
}
}
测试结果:
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=?
- Hibernate乐观锁实现之Timestamp
- Hibernate 乐观锁实现之 TimeStamp
- Hibernate 乐观锁之Timestamp
- Hibernate乐观锁之Timestamp注意点
- Hibernate乐观锁实现之Version
- Hibernate 乐观锁实现之 Version
- Hibernate乐观锁实现之Version
- Hibernate乐观锁实现之Version
- Hibernate 乐观锁实现之 Version
- Hibernate乐观锁实现之Version
- Hibernate 乐观锁实现之 Version
- Hibernate乐观锁实现之Version
- java Hibernate 配置 SqlServer数据库 timestamp乐观锁注意事项
- Hibernate乐观锁实现方式
- Hibernate乐观锁之Version
- Hibernate学习---第十三节:Hibernate之锁机制&乐观锁实现
- Hibernate乐观锁的实现原理
- Hibernate乐观锁实现——Version
- JUnit Test——访问私有成员(Field和Method)
- org.springframework.beans.NotWritablePropertyException: Invalid property... 问题解决
- Arguments
- 'ログオフ、シャットダウンの通知(メールの方式)
- 轻型web server比较
- Hibernate乐观锁实现之Timestamp
- crontab 执行php程序
- c#断点续传
- 用 Eclipse CFEclipse 插件快速编写 ColdFusion 应用程序
- C#编码标准--编码习惯
- 关于Spring MVC I18N 的配置问题
- pragma指令简介
- ASP AJAX用法
- 工作流系统参考模型