栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mysql处理insert冲突的解决方案

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mysql处理insert冲突的解决方案

1.采用insert on duplicate key 方法 

#展示有多少索引
show index from space_event_warn;

#删除指定表中的指定索引
drop index jkUniqueIndex_group_type on space_event_warn;

#添加唯一联合索引
alter table space_event_warn add unique index  jkUniqueIndex_group_type(event_group, event_type);

#insert on duplicate key 测试
insert into space_event_warn(event_type, event_group) values ("asd", "qqq")
on duplicate key
update  event_type = "123", event_group = "567";

2.通过检索判断方法


    
        select count(*) from info_metadata where product = #{product} and directory = #{directory}
    
    
        update  info_metadata
        
            
                product = #{product},
            
            
                size = #{size},
            
            
                directory = #{directory},
            
            
                creation_time = #{creationTime},
            
            
                identification = #{identification},
            
            
                product_level = #{productLevel},
            
            
                satellite = #{satellite},
            
            
                instrument = #{instrument},
            
            
                product_type = #{productType},
            
            
                file_type = #{fileType},
            
            
                data_time = #{dataTime},
            
            
                path = #{path},
            
            
                match_dir_flag = #{matchDirFlag},
            
            
                version = #{version},
            
            
                sha1 = #{sha1},
            
            
                suffix = #{suffix}
            
        
        where id = #{id}
    
    
        insert into  info_metadata(product, size, directory, creation_time, identification, product_level, satellite, instrument, product_type, file_type, data_time, path, match_dir_flag, version, sha1, suffix)
        values (#{product}, #{size}, #{directory}, #{creationTime}, #{identification}, #{productLevel}, #{satellite}, #{instrument}, #{productType}, #{fileType}, #{dataTime}, #{path}, #{matchDirFlag}, #{version}, #{sha1}, #{suffix})

    

 select  for update用法

select * from t for update 会等待行锁释放之后,返回查询结果。
select * from t for update nowait 不等待行锁释放,提示锁冲突,不返回结果
select * from t for update wait 5 等待5秒,若行锁仍未释放,则提示锁冲突,不返回结果
select * from t for update skip locked 查询返回查询结果,但忽略有行锁的记录

SELECT…FOR UPDATE 语句的语法如下:
  SELECT … FOR UPDATE [OF column_list][WAIT n|NOWAIT][SKIP LOCKED];
其中:
  OF 子句用于指定即将更新的列,即锁定行上的特定列。
  WAIT 子句指定等待其他用户释放锁的秒数,防止无限期的等待。

“使用FOR UPDATE WAIT”子句的优点如下:
  1防止无限期地等待被锁定的行;
  2允许应用程序中对锁的等待时间进行更多的控制。
  3对于交互式应用程序非常有用,因为这些用户不能等待不确定
  4 若使用了skip locked,则可以越过锁定的行,不会报告由wait n 引发的‘资源忙’异常报告

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985431.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号