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. Linux下iptables拦截Nginx的问题

    环境:CentOS 6.4 X64,Nginx 1.5.3 问题:配置好Nginx后,加入了“iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT” ...

  2. SQLServer处理亿万级别的数据的优化措施

    如何在SQLServer中处理亿万级别的数据(历史数据),可以按以下方面进行: 去掉表的所有索引 用SqlBulkCopy进行插入 分表或者分区,减少每个表的数据总量 在某个表完全写完之后再建立索引 ...

  3. allegro 16.6 空心焊盘的制作

    手机键盘的按键就是空心焊盘,新建一个外径为0.6mm 内径为0.4mm 的空心焊盘 空心焊盘的制作如下: 一.新建一个空心的shape 1 shape -> Cirrular 在坐标处输入 x ...

  4. c++数组-矩阵的转置

    #include <iostream> using namespace std; int main(){ ][]={{,,},{,,}}; ][]; ;j<;j++){ ;i< ...

  5. ListIterator-迭代器

    Iterable(lang)-->Iterator(util)-->ListIterator(util) method: 1.void add(E o) 2.boolean hasNext ...

  6. SQL SERVER 強制指定使用索引 -转载 只为学习

    今天很高兴 ,有学会了一种数据库优化的方式,哈哈 今天遇到一個查詢逾時的問題:兩段SQL,只差在WHERE,一個是WHERE COLUMN1='AAA',一個是WHERE COLUMN1='BBB', ...

  7. WPF九宫格HLSL版

    偶遇需要再WPF里面处理九宫格,因不喜截图缩放,即写成了HLSL的Effcect sampler2D input : register(s0); /// <summary>The floa ...

  8. 自定义PopupWindow 怎么设置PopupWindow的宽度充满全屏宽度

    自定义了一个MyPopMenu类,用于上图中的下拉筛选效果的. 但是按照网上有说需要: new PopupWindow(view,getWindowManager().getDefaultDispla ...

  9. SQL Server数据库事务日志存储序列

    原文 原文:http://blog.csdn.net/tjvictor/article/details/5251351   如果你的数据库运行在完整或是批量日志恢复模式下,那么你就需要使用作业(job ...

  10. 存储过程——在LINQ中使用(六)

    上述几篇都将了存储与数据库,关联的一些实例,首先感谢各位大神们在前几篇文章中提到的问题,本人还在学习中,这次介绍下在linq中如何应用存储过程: LINQ简介 语言集成查询(LINQ)在对象领域和数据 ...