10线程同时操作,频繁出现插入同样数据的问题。虽然在插入数据的时候使用了:
insert inti tablename(fields....) select @t1,@t2,@t3 from tablename where not exists (select id from tablename where t1=@t1,t2=@t2,t3=@t3)
当时还是在高并发的情况下无效。此语句也包含在存储过程中。(之前也尝试线判断有无记录再看是否写入,无效)。
因此,对于此类情况还是需要从数据库的根本来解决,就是约束。否则数据库的原子操作细不到我所需要的层面。 添加约束的命令行用得人不多,网上每次找SQL语句都累死,还是写下来好了。 需要的关键就叫做 字段组合约束唯一性
alter table tablename add CONSTRAINT NewUniqueName Unique(t1,t2,t3)