频道栏目
首页 > 资讯 > Java > 正文

玩转Spring事务(一)

13-06-14        来源:[db:作者]  
收藏   我要投稿

1.事务就是不成功便成仁的节奏

简单来说事务是由一系列要不都成功要不都失败的操作组成的,举个简单例子:小明进行转账处理

可以预见的是系统处理步骤是:

1.修改小明的账户余额:减少100;

2.修改对方的账户余额:增加100;

这时候如果在处理步骤2时,系统异常了,没有正常增加100,那么对么小明账户的操作也应该是无效的.

 


2.没有事务的世界

1.创建一个账户表:


[sql]
create table T_ACCOUNT 

  code    VARCHAR2(100) not null, 
  balance NUMBER, 
  owner   VARCHAR2(100) 

create table T_ACCOUNT
(
  code    VARCHAR2(100) not null,
  balance NUMBER,
  owner   VARCHAR2(100)
)
2.创建两个账号:


[sql] 
insert into t_account(code,balance,owner) values ('001',100,'小明'); 
insert into t_account(code,balance,owner) values ('002',100,'小张'); 

insert into t_account(code,balance,owner) values ('001',100,'小明');
insert into t_account(code,balance,owner) values ('002',100,'小张');现在 我们有两个账号了,
现在进行转账操作:


[sql] 
update t_account t  set t.balance = t.balance -100 where t.owner='小明'; 
commit; 
update t_account t  set t.balance = t.balance +100 where t.owner='小张 '; 
commit; 

update t_account t  set t.balance = t.balance -100 where t.owner='小明';
commit;
update t_account t  set t.balance = t.balance +100 where t.owner='小张 ';
commit;将小明的账号减100,小张的账号加100

看下对不对


[sql] 
select t.* from t_account t 

select t.* from t_account t
结果如下:

  
擦!小明的账号是减少了100,可小张的还是100,好吧,这里设了个小坑,故意把小张的名字后加了一个空格,这样的话肯定就不会对小张的账户进行更新了,但是由于进行了commit,所以小明的账户余额被莫名其妙地减少了100


3.与事务的初级见面

在SQL里输入以下代码:


[sql] 
declare 
 vCount number; 
begin 
  select count(*) into vCount from t_account t where t.code='小明'; 
  if vCount = 0 then  
    raise no_data_found; 
   end if;  
  update t_account t  set t.balance = t.balance -100 where t.owner='小明'; 
  select count(*) into vCount from t_account t where t.code='小张 '; 
    if vCount = 0 then  
    raise no_data_found; 
   end if;  
  update t_account t  set t.balance = t.balance +100 where t.owner='小张 '; 
  commit;  
exception 
  when no_data_found then 
    dbms_output.put_line('找不到对应账户'); 
    rollback; 
end; 

declare
 vCount number;
begin
  select count(*) into vCount from t_account t where t.code='小明';
  if vCount = 0 then
    raise no_data_found;
   end if;
  update t_account t  set t.balance = t.balance -100 where t.owner='小明';
  select count(*) into vCount from t_account t where t.code='小张 ';
    if vCount = 0 then
    raise no_data_found;
   end if;
  update t_account t  set t.balance = t.balance +100 where t.owner='小张 ';
  commit;
exception
  when no_data_found then
    dbms_output.put_line('找不到对应账户');
    rollback;
end;运行后查看结果,发现账户余额一切正常,但报错信息如期而至.

 

这里的关键词commit和rollback,如果一切正常最后程序进行了commit,如果没有找到账户,则会进行rollback,rollback的效果就是小明和小张的账户余额都是"回滚"到处理之前的状态;

 

4.原来事务管理就是commit和rollback的组合啊

如果你面对的数据是存放在数据库中的,那么事务对你来说就是各种commit和rollback的组合.

 


5.事务的特性ACID

ACID:

A是Atomicity的简写,原子性,是数据库能够进行操作的最小的逻辑单元,证明他包含的所有操作都是不可分割的。

C是Consistency的简写,一致性,就是他们要么同时全部成功,要么同时全部失败。成功与失败的操作保持一致

I是Isolation的简写,隔离性,指并发的事务是相互隔离的。即一个事务内部的操作及正在操作的数据必须封锁起来,不被其它企图进行修改的事务看到;

D是Durability的简写,持久性,当系统或介质发生故障时,确保已提交事务的更新不能丢失;

 

相关TAG标签
上一篇:SQL Server附加数据库失败的解决办法(3415错误)
下一篇:pipe/fifo 管道与有名管道
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站