mysql行级锁 java 实现

mysql行级锁:

  1. 数据库引擎: InnerDB,其他的不支持行锁
  2. 所在方法要加上事务注解, 必须加上: @Transactional(isolation = Isolation.READ_COMMITTED)
  3. 查询sql举例:

    select * from global_lock where object=#{object} for update;

    注意:
  4. timeout必须设置,否则,一旦行锁被锁定,另一个调用的方法将永久的阻塞。加了超时后,如果行锁被获取,则超时提示
  5. 数据表必须加索引,否则,将采用表级锁
  6. 行锁可以重复调,只要不是同一行,可以跨表

例子如下:
数据库

CREATE TABLE `global_lock` (
  `object` varchar(20) DEFAULT NULL,
  **UNIQUE KEY `idx_global_lock_object` (`object`)**
) ENGINE=InnoDB DEFAULT CHARSET=utf8

mysql行级锁 java 实现_第1张图片
项目
mysql行级锁 java 实现_第2张图片
GlobalLockController .java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sm.rowlock.mapper.GlobalLockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class GlobalLockController {
    private static Logger logger = LoggerFactory.getLogger(GlobalLockController.class);
    
    @Autowired
    private GlobalLockMapper globalLockMapper;
    
    @RequestMapping("/getLock")
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void getLock() {
        globalLockMapper.getLock("object");
        System.out.println("111");
    }
    
    @RequestMapping("/getLock2")
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public void getLock2() {
        try {
            globalLockMapper.getLock("object");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("111");
    }
}

GlobalLockEntity .java

public class GlobalLockEntity {
    private String object;

    public String getObject() {
        return object;
    }

    public void setObject(String object) {
        this.object = object;
    }
}

GlobalLockMapper .java

import org.apache.ibatis.annotations.Param;

public interface GlobalLockMapper {
    void getLock(@Param("object")String object);
}

MainApp.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@tk.mybatis.spring.annotation.MapperScan(basePackages="org.sm.rowlock.mapper")
public class MainApp {

    public static void main(String[] args) {
        SpringApplication.run(MainApp.class, args);
    }
}

GlobalLockMapper.xml





    
        
    
    
    

application.properties



server.port=8081

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=user
spring.datasource.password=password


mybatis.type-aliases-package=org.sm.rowlock.entity
mybatis.mapper-locations=classpath:mappers/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

mapper.mappers=tk.mybatis.mapper.common.Mapper
mapper.not-empty=false
mapper.identity=MYSQL

你可能感兴趣的:(java基础,数据库集合)