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. JS定时器实例解析

    在javascritp中,有两个关于定时器的专用函数. 分别为:1.倒计定时器:timename=setTimeout("function();",delaytime);2.循环定 ...

  2. this.IsMounted() is not a function

    I'm trying to build a simple React App. It retrieves data from an ajax call and renders it to the pa ...

  3. 获取枚举Description的Name

    /// <summary> /// 获取枚举Description的Name /// </summary> /// <param name="enumName& ...

  4. 用户登录密码RSA加密后传输的实现,非明文密码传输

    在用户登录页面,用户输入密码后,在传送到服务器端时,为防止在密码传送过程中,被如360这种东东给拦截到, 需要在传送前对密码进行加密,然后再传送! 利用RSA加密,在客户端使用公钥对密码进行加密,在服 ...

  5. Spark 大数据平台

    Apache Spark is an open source cluster computing system that aims to make data analytics fast - both ...

  6. WPF学习02:Routed Events

    与传统的桌面开发相比,在事件模型上WPF引入了Routed Events,从开发者的角度上,我们获得了两个便利: 1.可以实现事件路由,即向XAML结构中的父元素路由或者是向子元素路由. 2. Rou ...

  7. 多实例MySQL批量添加用户和密码并授权

    OS:Cent OS 6.3 DB:5.5.14 如果一台服务器上有100个MySQL数据库实例,此时需要给不同端口的几个数据库添加用户名.密码并授权应用IP,一个个授权没问题,反正累的不是我,哇咔咔 ...

  8. 将Vim改造为强大的IDE

    1.安装Vim和Vim基本插件 首先安装好Vim和Vim的基本插件.这些使用apt-get安装即可: lingd@ubuntu:~/arm$sudo apt-get install vim vim-s ...

  9. UIActivityIndicatorView的使用

    class ViewController: UIViewController,UIActionSheetDelegate{ @IBOutlet weak var label1: UILabel! @I ...

  10. Aspose 数字和日期 设置

    Microsoft Excel一个非常强大的功能就是使客户可以设置数字和日期的显示格式,众所周知数字可以显示为不同的值格式,包含:小数.货币.百分数.分数.账面价值等,同样地Aspose.Cells也 ...