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.通过检索判断方法

<insert id="saveOrUpdata" parameterType="com.file..InfoMetadata">
    <selectKey keyProperty="count" resultType="int" order="BEFORE">
        select count(*) from info_metadata where product = #{product} and directory = #{directory}
    </selectKey>
    <if test="count > 0">
        update  info_metadata
        <set>
            <if test="product != null">
                product = #{product},
            </if>
            <if test="size != null">
                size = #{size},
            </if>
            <if test="directory != null ">
                directory = #{directory},
            </if>
            <if test="creationTime != null">
                creation_time = #{creationTime},
            </if>
            <if test="identification != null">
                identification = #{identification},
            </if>
            <if test="productLevel != null ">
                product_level = #{productLevel},
            </if>
            <if test="satellite != null ">
                satellite = #{satellite},
            </if>
            <if test="instrument != null">
                instrument = #{instrument},
            </if>
            <if test="productType != null ">
                product_type = #{productType},
            </if>
            <if test="fileType != null ">
                file_type = #{fileType},
            </if>
            <if test="dataTime != null">
                data_time = #{dataTime},
            </if>
            <if test="path != null">
                path = #{path},
            </if>
            <if test="matchDirFlag != null">
                match_dir_flag = #{matchDirFlag},
            </if>
            <if test="version != null ">
                version = #{version},
            </if>
            <if test="sha1 != null ">
                sha1 = #{sha1},
            </if>
            <if test="suffix != null ">
                suffix = #{suffix}
            </if>
        </set>
        where id = #{id}
    </if>
    <if test="count == 0">
        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})

    </if>
</insert>

 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 引发的‘资源忙’异常报告

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值