zoukankan      html  css  js  c++  java
  • Mysql 行锁 for update

    Mysql 只有Innodb支持行锁

    使用行锁需要 事务支持

    首先打开两个 mysql-client

    分别执行

    - client1
    select * from my_entity1 for update;
    - client2
    select * from my_entity1 for update;
    

    发现行锁无效,说明需要事务支持

    - client1
    start transaction;
    select * from my_entity1 for update;
    - client2
    select * from my_entity1 for update;
    

    这个时候 client2 阻塞等待锁

    此时给client1 输入 commit; client2获得锁并且获取结果

    如果client2 不加行锁也是不会被阻塞的

    除此之外 forupdate还有其他方法

    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 查询返回查询结果,但忽略有行锁的记录

    Spring jpa 行锁

    1. 对调用的service 加 @Transaction注解
    2. 使用 @Query 原生sql注解
    @Transactional
    public void updateLockTester()
    
    public interface Entity2Dao extends JpaRepository<MyEntity2, Long> {
        List<MyEntity2> findAllByTagEquals(Integer tag);
    
        @Query(value = "select * from my_entity2 where tag = :tag for update", nativeQuery = true)
        List<MyEntity2> findAllByTag2Equals(@Param("tag") Integer tag);
    }
    

    不能太过于依赖行锁,更建议使用分布式锁提高效率

  • 相关阅读:
    监控网页是否有变化
    设置开机自动启动进程
    nagios-调用脚本
    连接数据库出现10061错误
    小程序修改默认的radio样式
    小程序端,做类似于支付宝充值话费或流量的样式
    jq 在字符串中,去掉指定的元素
    vue 使用 proxyTable 解决跨域问题
    vue-cli 动态绑定图片失败
    vue-cli 使用 font-awesome 字体插件
  • 原文地址:https://www.cnblogs.com/stdpain/p/11016062.html
Copyright © 2011-2022 走看看