spring事务的问题,隔离级别设置成SERIALIZABLE还是会有重复数据

我贴一下代码

@Transactional(isolation = Isolation.SERIALIZABLE)

public String findCurrentBarcode(String unitsCode) {

TCurrentBarcodeExample tCurrentBarcodeExample = new TCurrentBarcodeExample();

tCurrentBarcodeExample.createCriteria().andCbUnitsCodeEqualTo(unitsCode);

String allBarcode = "";

String barcode = "";

List<TCurrentBarcode> dataList = tCurrentBarcodeMapper.selectByExample(tCurrentBarcodeExample);

logger.info("条码号:=====" + dataList.get(0).getCbCurrentNo());

if (dataList.size() > 0) {

TCurrentBarcode tCurrentBarcode = dataList.get(0);

TCurrentBarcode tCurrentBarcodeNew = new TCurrentBarcode();

tCurrentBarcodeNew.setCbId(tCurrentBarcode.getCbId());

tCurrentBarcodeNew.setCbUnitsCode(tCurrentBarcode.getCbUnitsCode());

barcode = tCurrentBarcode.getCbCurrentNo();

allBarcode = prefixBarcode + barcode;

while (true) {

TRequestExample tRequestExample = new TRequestExample();

tRequestExample.createCriteria().andRTestNoEqualTo(allBarcode);

List<TRequest> requests = tRequestMapper.selectByExample(tRequestExample);

if (requests.size() == 0) {

tCurrentBarcodeNew.setCbCurrentNo(String.valueOf(Long.valueOf(barcode) + 1));

tCurrentBarcodeMapper.updateByPrimaryKeySelective(tCurrentBarcodeNew);

return allBarcode;

}

barcode = String.valueOf(Long.valueOf(barcode) + 1);

allBarcode = prefixBarcode + barcode;

}

} else {

barcode = "9100000001";

TCurrentBarcode tCurrentBarcodeNew = new TCurrentBarcode();

long id = ibaseToolDao.getSequenceNextValue("SEQ_T_CURRENT_BARCODE");

tCurrentBarcodeNew.setCbId(String.valueOf(id));

tCurrentBarcodeNew.setCbUnitsCode(unitsCode);

tCurrentBarcodeNew.setCbCurrentNo(String.valueOf(Long.valueOf(barcode) + 1));

tCurrentBarcodeMapper.insert(tCurrentBarcodeNew);

return prefixBarcode + barcode;

}

}

调用方是一个有锁的方法

private synchronized String getBarCode(String unitsCode){

return iHisTestAppService.findCurrentBarcode(unitsCode);

}

但是这样子写还是会有重复的条码号出来,一天了都没弄好,感觉快废了


回答:

我觉得你可以先把你的业务逻辑说一下,光看代码不知道你要干什么,感觉可能是你业务代码写的有问题


回答:

看着没啥问题,不行试试数据库加唯一键试试,我感觉就是业务代码的问题,看着有锁有事务理论上应该不会

以上是 spring事务的问题,隔离级别设置成SERIALIZABLE还是会有重复数据 的全部内容, 来源链接: utcz.com/p/944111.html

回到顶部