当存在并发对同一个订单都做更想操作时,怎么才能保证各自的线程有效的执行呢?
采用悲观锁处理方式:@Lock(LockModeType.PESSIMISTIC_WRITE)
AreaPO.java
@Data
@EqualsAndHashCode()
@Accessors(chain = true)
@Entity
@Table(name = "AREA_INFO")
public class AreaInfoPO {
/**
* 区域主键
*/
@Id
@Column(name = "AREA_ID", length = 50)
@GeneratedValue(generator = "idGenerator")
@GenericGenerator(name = "idGenerator", strategy = com.tz.common.util.IdGenerator", parameters = {
@org.hibernate.annotations.Parameter(name = "prefix", value = "area")})
private String areaId;
/**
* 可用状态:1-可用,0-不可用
*/
@Column(name = "AVAIL_STATE", length = 1)
private String availState;
/**
* 区域名称
*/
@Column(name = "AREA_NAME", length = 128)
private String areaName;
/**
* 区域类型:1-总行,2-分行
*/
@Column(name = "AREA_TYPE", length = 1)
private String areaType;
/**
* 区域描述
*/
@Column(name = "AREA_DESC", length = 1024)
private String areaDesc;
/**
* 创建时间
*/
@Column(name = "CRT_TIME")
private LocalDateTime crtTime;
/**
* 上级区域
*/
@Column(name = "PARENT_AREA_ID", length = 50)
private String parentAreaId;
/**
* 区域序号
*/
@Column(name = "NUM", length = 11)
private Integer num;
}
AreaVO.java
@Data
@ApiModel(description = "申请单VO")
public class AreaInfoVO {
@ApiModelProperty("区域主键")
private String areaId;
@ApiModelProperty("可用状态:1-可用,0-不可用")
private String availState;
@ApiModelProperty("可用状态中文名")
private String availStateName;
@ApiModelProperty("区域名称")
private String areaName;
@ApiModelProperty("区域类型:1-总行,2-分行")
private String areaType;
@ApiModelProperty("区域类型中文名")
private String areaTypeName;
@ApiModelProperty("区域描述")
private String areaDesc;
@ApiModelProperty("创建时间")
private LocalDateTime crtTime;
@ApiModelProperty("上级区域")
private String parentAreaId;
@ApiModelProperty("上级区域名称")
private String parentAreaName;
@ApiModelProperty("区域序号")
private Integer num;
}
AreaInfoServiceImp.java
@Override
@Transactional
public void testBatch() {
AreaInfoVO areaInfoPO = mapper.poToVO(areaInfoDao.queryById("area-123"));
if (areaInfoPO != null){
log.info("num = {}",areaInfoPO.getNum());
areaInfoPO.setNum(areaInfoPO.getNum()+1);
areaInfoDao.save(mapper.voToPO(areaInfoPO));
}
}
AreaIndoDao.java
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select s from AreaInfoPO s where s.areaId = :#{#id} ")
AreaInfoPO queryById(String id);
Test.java
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(15);
for (int i = 0; i < 15; i++) {
executor.submit(new R());
}
}
}
class R implements Runnable {
@Override
public void run() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getForObject("http://127.0.0.1:8080/aweb/areaInfo/test", String.class);
}
}
版权声明:本文为qq_34254462原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。