in

SDT Community Server

SDT Forums, Blogs, Photos server.

wego

Oracle 处理数据高并发响应

某些系统可能对系统应付数据高并发的响应要求比较高 (如: 飞行航空订票系统). 本文采用 Oracle 的 lock table 解决之.

Procedure 中关键 code 如下:

-------------------------------------------------------------

lock table tb_book_ticket in share mode;

...

insert into tb_book_ticket (id, scheduled_flight, ticket_no, book_time) values (ID.nextval, v_scheduled_flight, v_ticket_no, SYSDATE);    -- mark1

...    -- mark2

select count(*) into v_book_count from tb_book_ticket where scheduled_flight = v_scheduled_flight;    -- mark3

if v_book_count <= v_book_max_count then

    v_result := 'book successfully';

    commit;

else

    v_result := 'book unsuccessfully';

    rollback;

end if;

return v_result;

-------------------------------------------------------------

以上有2个地方需注意:

1) 采用 lock table table_name in share mode 方式而非 lock table table_name in exclusive mode 提高并发性处理, 尽量减少不必要执行等待.

2) 按常规思路, 可能会将 mark1 与 mark3 顺序交换, 但此举不行

-------------------------------------------------------------

select count(*) into v_book_count from tb_book_ticket where scheduled_flight = v_scheduled_flight;    -- mark3

...    -- mark2

if v_book_count < v_book_max_count then

    insert into tb_book_ticket (id, scheduled_flight, ticket_no, book_time) values (ID.nextval, v_scheduled_flight, v_ticket_no, SYSDATE);    -- mark1

     -- mark4

    commit;

end if;

-------------------------------------------------------------

试想若 userA 执行到  mark4, 而同时 userB 执行到  mark3, 就可能会出现由于 userA  尚未commit 而导致 userB 订到机票但实际没有座位的情况.

若确希望使用常规逻辑来实现, 需要用到 Oracle 的表的读锁功能 (精细访问策略), 而此非本文范围, 且其对于SYS用户无效.

 

Published Jul 03 2008, 09:00 AM by wego
Filed under:

Comments

No Comments
Copyright SDT, 2006-2009. All rights reserved.