当数据库并发时。我只能允许插入一条记录,如何防止插入多条。。。
insert into tmp_dim
select 1,'A' from dual where not exists (select 1 from tmp_dim where id=1);
如果开启事务的时候,两条数据执行到这个语句时都没有获得提交,就会插入两条。。
当数据库并发时。我只能允许插入一条记录,如何防止插入多条。。。
insert into tmp_dim
select 1,'A' from dual where not exists (select 1 from tmp_dim where id=1);
如果开启事务的时候,两条数据执行到这个语句时都没有获得提交,就会插入两条。。
1 用Oracle约束
2 设定一个更好的业务模型
3 应用层锁
1. 加redis分布式锁
2. 设置唯一索引, 重复插入的时候可以报错