1.

2.

 hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/sampledb
hibernate.connection.username=root
hibernate.connection.password=1234
hibernate.show_sql=true
hibernate.connection.isolation=2

3.

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping > <class name="mypack.Monkey" table="MONKEYS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="name" type="string" column="NAME" /> <property name="count" type="int" column="COUNT" /> </class>
</hibernate-mapping>

4.

 package mypack;

 import java.util.Set;

 public class Monkey  {

     private Long id;

     private String name;

     private int count;

     public Monkey(String name, int count) {
this.name = name;
this.count = count;
} public Monkey() { } public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public int getCount() {
return this.count;
} public void setCount(int count) {
this.count = count;
} }

5.

 package mypack;

 import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*; public class BusinessService extends Thread{
public static SessionFactory sessionFactory;
static{
try{
Configuration config = new Configuration();
config.addClass(Monkey.class); sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;}
} private Log log; public BusinessService(String name,Log log){
super(name);
this.log=log;
} public void run(){
try{
vote();
}catch(Exception e){
e.printStackTrace();
}
} public void vote()throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try { tx = session.beginTransaction();
log.write(getName()+":开始事务");
Thread.sleep(500); Monkey monkey=(Monkey)session.get(Monkey.class,new Long(1),LockMode.UPGRADE);
//Monkey monkey=(Monkey)session.get(Monkey.class,new Long(1));
log.write(getName()+":查询到智多星的票数为"+monkey.getCount());
Thread.sleep(500); monkey.setCount(monkey.getCount()+1);
log.write(getName()+":把智多星的票数改为"+monkey.getCount()); log.write(getName()+":提交事务");
tx.commit(); Thread.sleep(500); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public static void main(String args[]) throws Exception {
Log log=new Log();
Thread thread1=new BusinessService("猴子甲投票事务",log);
Thread thread2=new BusinessService("猴子乙投票事务",log); thread1.start();
thread2.start(); while(thread1.isAlive() ||thread2.isAlive()){
Thread.sleep(100);
}
log.print();
sessionFactory.close();
}
} class Log{
private ArrayList logs=new ArrayList(); synchronized void write(String text){
logs.add(text);
}
public void print(){
for (Iterator it = logs.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}
}

6.

 drop database if exists SAMPLEDB;
create database SAMPLEDB;
use SAMPLEDB; drop table if exists MONKEYS ;
create table MONKEYS(
ID bigint not null,
NAME varchar(15),
COUNT int,
primary key (ID)
) type=INNODB; insert into MONKEYS(ID,NAME,COUNT) values(1,'智多星',1000);

Hibernate逍遥游记-第15章处理并发问题-002悲观锁的更多相关文章

  1. Hibernate逍遥游记-第15章处理并发问题-003乐观锁

    1. 2. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; drop table if exists ...

  2. Hibernate逍遥游记-第15章处理并发问题-001事务并发问题及隔离机制介绍

    1. 2.第一类丢失更新 3.脏读 4.虚读.幻读 5.不可重复读 6.第二类丢失更新 7.数据库的锁机制 8.数据库事务的隔离机制

  3. Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  4. Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  5. Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  6. Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  7. Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  8. Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多

    0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...

  9. Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

随机推荐

  1. ASP.NET MVC 2 验证

    来源:http://www.cnblogs.com/jhxk/articles/2612885.html  只为把自己觉的好的存起来 对用户输入的验证以及强制业务规则/逻辑是大多数web应用的核心需求 ...

  2. hadoop分布式安装过程

    一.安装准备及环境说明 1.下载hadoop-1.2.1,地址:http://apache.spinellicreations.com/hadoop/common/stable/hadoop-1.2. ...

  3. openerp学习笔记 跟踪状态,记录日志,发送消息

    跟踪状态基础数据: kl_qingjd/kl_qingjd_data.xml <?xml version="1.0"?><openerp>    <d ...

  4. python autopy

    今天学习了python autopy 主要包括alert,color,mouse,key,bitmap,screen等的操作 文档在http://www.autopy.org/documentatio ...

  5. 【Web学习日记】——C#引用WebService,从配置文件改变引用地址

    开发环境:Win7 32位,开发工具:VS2013,.Net:4.0 初用WebService,很多地方都搞不清楚怎么回事,但稍作研究之后,也就用上了,根本就没有考虑后续事情. 但是,随着项目的进行, ...

  6. Microsoft Azure 全球状态

    除了mooncake的中国大陆区域 Azure status http://azure.microsoft.com/en-us/status/?rnd=1#current http://azure.m ...

  7. linux 线程笔记

    线程与进程关键字对比 创建新流 fork/pthread_create 退出控制流 exit/pthread_exit 获取退出状态 waitpid/pthread_join 在退出时的清理工作 at ...

  8. C#二维数组及其本质(转)

    C#中二维数组包含两类:二维数组和数据矩阵.(这是我个人分类法,我认为比较能反映本质). 如上图,是二维数组,横向为第一维度,纵向为第二维度,不同维度可以有不同长度. 如果去掉元素7,那么上图也可能是 ...

  9. Win7下配置nginx和php5

    本文链接:http://www.cnblogs.com/cnscoo/archive/2012/09/03/2668577.html 一.准备工作: OS:Windows7 SP1 Nginx: ng ...

  10. Java反射在JVM的实现

    1. 什么是Java反射,有什么用?反射使程序代码能够接入装载到JVM中的类的内部信息,允许在编写与执行时,而不是源代码中选定的类协作的代码,是以开发效率换运行效率的一种手段.这使反射成为构建灵活应用 ...