您的位置:整机首页 >> 服务器 >> 技巧 >> 正文

在OLTP的环境下使用大事务出现的问题 (2)

2008-02-14 13:12:00 作者:佚名 关注: 50

核心提示:在应用环境中,常常需要保证几张表相关数据的一致性,为了应对这种需求,开发工程师常常会使用事务,把一些insert,update等语句绑在一起

友情提示: 购买电脑手机、数码、家电等产品时提及您是广安IT网网友将获得优惠。

begin transaction1_1
      update thread set post_count=nvl(post_count,0) + 1 where id=#v_id;
end transaction1_1

begin transaction1_2
      insert into messgae(id,col1,col2...) values(#id,#col1,#col2...);
      update test_user set sum_reply = sum_reply + 1 where userid=#v_userid;
end transaction1_2

此方案已经比原有完全拆分事务,在数据一致性保护上有了很大的改进,但还可以进一步改进,这个改进在应用层做,示例如下:

begin transaction1_1
      update thread set post_count=nvl(post_count,0) + 1 where id=#v_id;
end transaction

if transaction1_1 执行成功 then

      begin transaction1_2
            insert into messgae(id,col1,col2...) values(#id,#col1,#col2...);
            update test_user set sum_reply = sum_reply + 1 where userid=#v_userid;
      end transaction
      exception when others then
            print insert SQL into log;
            print update SQL into log;
            将log信息发送报警邮件,此SQL可以手动重新执行,这种情况是比较少见的,但出现了,我们是可以进行数据订正的
            print another way  
            if you don't want to execute insert SQL,update SQL,you can execute the SQL mannul as follows:
               update thread set post_count=nvl(post_count,0) - 1 where id=#v_id;
            end if;
            注:恢复数据一致性的方法有两种,根据实际情况恰当选择    
end if;

把大事务根据业务规则合理拆分成有一定组合的小事务,在小事务内部由数据库保证数据的一致性,并在这些小事务之间在应用层设计一定的出错控制,既解决了数据库因OLTP高并发访问所导致的锁阻塞问题,也尽可能的在最大程度上保证应用层数据的一致性。

川东地区电子产品购买免费服务咨询电话:0826-4899111转4,QQ群号:49049457

[1] [2]
  • 0

    顶一下

  • 0

    踩一下

Google
责任编辑:yang912
  • 作者:佚名 来 源:互联网
  • >> 相关资讯

    • 全部问题
    • 推荐问题
    • 高分问题
    • 精彩图文
      近期推荐
      热门产品排行
      软件下载
      友情推荐