说明:tcount 表中有两个字段x记录是流水号,item是类型思路:根据不同的item取相应的流水号,流水号是递增的--取流水号,如何解决并发?
procedure getX(p_item varchar2,cu out number)
is
p number;
begin
p:=0;
cu:=1;
select count(*) into p from tcount where item=p_item;
if p>0 then
begin
select x+1 into cu from tcount where item=p_item;
update tcount set x=x+1 where item=p_item;
end;
else
begin
insert into tcount values(1,p_item);
end;
end if;
commit;
end;
procedure getX(p_item varchar2,cu out number)
is
p number;
begin
p:=0;
cu:=1;
select count(*) into p from tcount where item=p_item;
if p>0 then
begin
select x+1 into cu from tcount where item=p_item;
update tcount set x=x+1 where item=p_item;
end;
else
begin
insert into tcount values(1,p_item);
end;
end if;
commit;
end;
解决方案 »
- 数据库中numberic 和number的区别
- 怎样给视图里面的字段加comments(备注)
- 菜鸟提问?
- 我在pl sql develope 中创建了一个存储过程,让后执行,问什么显示不正确,存储过程进来看
- 局域网内,知道某服务器的ip,windows下如何知道该服务器上的oracle 数据库名 和所在端口?谢谢各位朋友
- sql 关于to_date日期格式问题求助
- 建立一个过程,提示出错,请看图
- windows下如何实现oracle数据库的自动备份?
- C#连接oracle数据库进行添加用户名和密码,还要检测用户名是否已经存在
- 在Oracle中能否只建一用户、建多个表空间呢
- 请教一条sql语句如何写。
- 读取clob类型的字段内容时出错
看下oracle sequence
1.加上自治事务。
2.对表tcount使用for update行锁定
如果无意义,则可以使用sys_guid()来生成无意义的、不重复的值。Insert into customer (id_cust, cust_name) values ( sys_guid() ,’Jimmy’);
注意: 如果要使用主键ID值作为条件查询,必须使用hextoraw函数才会用到主键索引。例如: WHERE ID=hextoraw('A36072E794E35537E04400144F674217') ; 否则不会使用主键索引,带来性能问题。
另外,利用for update不会引起并发问题啊。因为锁定是针对某个item的所有数据,因此,不会存在并发问题。对于同一个item的多事务串行是必需的。另外,可能需要注意,LZ取记录数目的做法并不可取,想象用户删除数据的情况下。肯定会出现重号。
用for update是,对item已存在的类型来说,是可以解决,
但如果最初tcount为空时,是有插入动作的,如果这里并发,如何处理呢?
代码如下:
...
else
begin
insert into tcount values(1,p_item);
end;
end if;
commit;
end;
把代码贴出来,欢迎拍砖。
相信取流水号对许多项目是有帮助.--取顺序流水号
procedure getX(p_item varchar2,cu out number)
is begin cu:=0;
autonomous_insert(p_item);
select x+1 into cu from tcount where item=p_item for update;
update tcount set x=x+1 where item=p_item;
commit;
end;
procedure autonomous_insert(p_item varchar2)
is
--使用自治事务
pragma autonomous_transaction;
begin
insert into tcount
select 0,p_item
from tcount
where not exists(select 1 from tcount t where t.item=p_item)
and rownum=1; commit;
exception when others then
rollback;
end;
end aa;
procedure autonomous_insert(p_item varchar2)
is
--使用自治事务
pragma autonomous_transaction;
begin
insert into tcount
select 0,p_item
from dual where not exists(select 1 from tcount t where t.item=p_item); commit;
exception when others then
rollback;
end;
利用这个方法啊,那不是每次去拿序列号的时候,都会尝试向表中插入数据?
会影响效率的吧。LZ是利用唯一性约束item来保证?