乐观锁实现高并发

场景:收货操作,对收货明细做修改操作,两个人同时对某一变量进行修改(a=10),都想在原来基础上+10,会存在两人同时修改,最终结果还是20,正确结果应该是30。

实现:通过乐观锁实现修改

实现方式:

1,修改时把修改内容作为条件

2,加版本号

以上都需要先对数据进行查询,获取老数据的“修改内容”和版本号,以此作为修改的条件。

代码

1,实现方式通用mapper或者sql

通用mapper

	OutinvEntity param = new OutinvEntity();
			param.setLoadNo(outinvDetailEntity2.getLoadNo());
			param.setFactoryAreaId(factoryAreaId);
			param.setOrgId(orgId);
			OutinvEntity resEntity = outinvMapper.selectOne(param);
            if (resEntity == null) {
                throw new RuntimeException("OutinvEntity is null");
            }
			
			OutinvEntity outinvEntitys = new OutinvEntity();
			outinvEntitys.setLoadNo(outinvDetailEntity2.getLoadNo());
			outinvEntitys.setIsPrint(OutinvIsPrintE.YES.getValue());

			outinvEntitys.setStatus(OutinvStatusE.DELIVERYSTORAGE.getValue());
			outinvEntitys.setShipTime(new Date());
			outinvEntitys.setUpdateTime(new Date());
			outinvEntitys.setUpdateUser(request.getUserName());
		     
            Example example = new Example(OutinvEntity.class);
            example.createCriteria()
               .andEqualTo("loadNo", outinvDetailEntity2.getLoadNo())
               .andEqualTo("status", resEntity.getStatus());
            int i = outinvMapper.updateByExampleSelective(outinvEntitys, example);
			 if (i < 1) {
                 throw new RuntimeException("Failed to update library status");
             }

sql

<!-- 乐观锁更新 -->
	<update id="updateByPrimaryKeySelectives" parameterType="com.anji.allways.business.inv.vo.OutinvVO">
		update wms_outinv
		<set>
			<if test="status != null">
				status = #{status},
			</if>
			<if test="shipOperator != null">
				ship_operator = #{shipOperator},
			</if>
			<if test="shipTime != null">
				ship_time = #{shipTime},
			</if>
			<if test="rcvMan != null">
				rcv_man = #{rcvMan},
			</if>
			<if test="isPrint != null">
				is_print = #{isPrint},
			</if>
			<if test="updateTime != null">
				update_time = #{updateTime},
			</if>
			<if test="updateUser != null">
				update_user = #{updateUser},
			</if>
			<if test="handoverTime != null">
				handover_time = #{handoverTime},
			</if>
			<if test="handoverUser != null">
				handover_user = #{handoverUser},
			</if>
				<if test="receiverTime != null">
				receiver_time = #{receiverTime},
			</if>
			<if test="receiverUser != null">
				receiver_user = #{receiverUser},
			</if>
			<if test="dueDate !=null">
				due_date=#{dueDate},
			</if>
		</set>
		<where>
		<if test="id !=null">
			and id = #{id}
		</if>
		<if test="loadNo != null">
			and load_no = #{loadNo}
		</if>
		<if test="orgId != null">
			and org_id = #{orgId}
	    </if>
	    <if test="oldDueDate !=null">
			and	due_date=#{oldDueDate}
		</if>
		<if test="status != null">
			and	status = #{oldStatus}
		</if>
	    </where>
	</update>

2,版本号

sql

        UPDATE
          wms_rcv_doc_detail
        SET
          received_num = #{receivedNum},
          status = #{status},
          rcv_report_status = #{rcvReportStatus},
          should_report_rcv_num = #{shouldReportRcvNum},
          update_time = #{updateTime},
          update_user = #{updateUser},
          <if test="receiveTime != null and receiveTime != ''">
          	receive_time = #{receiveTime},
          </if>
          version = #{version}+1    
        <where>
            id = #{id}
            AND version = #{version}
        </where>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值