• 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);
    }
    

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

  • 相关阅读:
    Android深度探索-卷1 第三章
    Android深度探索-卷1 第四章
    Android深度探索-卷1 第二章
    Android深度探索-卷1 第一章
    硬件抽象层HAL
    嵌入式Linux调试技术
    蜂鸣器驱动
    控制发光二极管
    搭建S3C6410开发板的测试环境
    统计单词个数
  • 原文地址:https://www.cnblogs.com/stdpain/p/11016062.html
Copyright © 2020-2023  润新知