数据库:
create table account (
id int primary key auto_increment,
name varchar(20),
money double
);
insert into account values(null,‘a‘,100);
insert into account values(null,‘b‘,100);
1.演示脏读
开两个client的窗体,Aclient的级别是数据库默认的Repeatable read -- 可以防止脏读/不可反复读问题,可是不能防止虚读(幻读)问题。Bclient的级别更改为效率最高的Read uncommitted级别 不可以解决脏读的问题
A窗体:
开启事务:start transaction;
a账户转账10元到b账户:update account set money=money-10 where name=‘a‘;
b账户添加10元:update account set money=money+10 where name=‘b‘;
不提交事务;
B窗体:
开启事务:start transaction;
查询数据库中的b账户数据:select * from account;
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDAxMDc2OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >
A窗体:
回滚事务:rollback;
B窗体:
查询数据:select * from account;
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDAxMDc2OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >
脏读数据产生。
解决方式:
B窗体的隔离级别改为Read committed
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDAxMDc2OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >