请教,如何处理排队抢号?

[复制链接]
查看11 | 回复2 | 2014-2-18 16:41:11 | 显示全部楼层 |阅读模式
假如我现在有一个预生成的号码表,
TABLE A
编号使用标志
0001
0002
0003
0004
....
9999
我现在做了个程序,多用户同时操作,要去使用这些号码,被人使用后,要更新使用标志为1,其它人不能再被使用。
请问,我该怎么处理,谢谢大家

回复

使用道具 举报

千问 | 2014-2-18 16:41:11 | 显示全部楼层
没做过开发,瞎说一下:
0未使用
1正在被选中
2已经被使用
一个session进入选号过程的时候,先 update=1,选完update=2
展示候选号码时 where 标志位=0
回复

使用道具 举报

千问 | 2014-2-18 16:41:11 | 显示全部楼层
select 编号 from a where 使用标志1
and rownum <=1(随机取一个号)
FOR UPDATE NOWAIT SKIP LOCKED; (11.2以前版本)
用号。。。。。。。
update a set使用标志=1 where 编号= 上面取到的号

注意事务一致性即可。
Oracle Oracle 11.2 Update: The syntax "NOWAIT SKIP LOCKED" is not accepted in Oracle 11.2. The "NOWAIT" keyword and "SKIP LOCKED" keyword have become mutually exclusive. Remove the "NOWAIT" if you want to use "SKIP LOCKED".
By default, the transaction waits until the requested row lock is acquired. If you are not willing to wait to acquire the row lock, use either the NOWAIT clause of the LOCK TABLE statement (see Choosing a Locking Strategy) or the SKIP LOCKED clause of the SELECT FOR UPDATE statement.
If you can lock some of the requested rows, but not all of them, the SKIP LOCKED option skips the rows that you cannot lock and locks the rows that you can lock.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行