我有一个带有几个方法的@Service 层类,其中有@Transactional(isolation = Isolation.READ_UNCOMMITTED) 注释查询和@Transactional 注释写入事务。有时事务上会出现死锁,我可以在 SQLServer Profiler 死锁分析 XML 中看到,查询具有 READ_COMMITED 隔离级别。如何更改事务的隔离级别设置?欢迎任何帮助。
有这样的存储库:
@Repository
public interface mStorageRepository extends AdBaseClientOrgRepository<mStorage, Long> {
@Modifying
@Query("UPDATE mStorage s SET s.qtyonhand = s.qtyonhand + ?2, s.updated = current_timestamp WHERE s.id = ?1")
void updateqtyBymStorageid(long id, BigDecimal movementqty);
@Query("SELECT s FROM mStorage s WHERE s.locator.id = ?1 AND s.stockAlert = ?2 order by s.datelastinventory")
Iterable<mStorage> getByLocatorAndStockAlert(long mLocatorId, String stockAlert);
.
.
.
}
有这样的日期服务层:
@Service
public class mStorageService extends AdBaseClientOrgService<mStorage, mStorageRepository> {
@Autowired
public mStorageService(Logger logger, TimeService timeService, MessageService messageService) {
super(logger, timeService, messageService);
}
@Override
@Autowired
public void setRepo(mStorageRepository repo) {
this.repo = repo;
}
@Transactional
public void updateqtyBymStorageid(long id, BigDecimal movementqty) {
logger.debug("start updateqtyBymStorageid : {} , {}",id,movementqty);
repo.updateqtyBymStorageid(id, movementqty);
logger.debug("end updateqtyBymStorageid : {} , {}",id,movementqty);
}
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public Iterable<mStorage> getByLocatorAndStockAlert(long locatorId, StockAlert stockAlert) {
Iterable<mStorage> ret = this.repo.getByLocatorAndStockAlert(locatorId, stockAlert.toString());
getLazyProperties(ret);
return ret;
}
.
.
.
}
还有几个业务逻辑服务层实现:
@Service
public class BaseProductionPartsUsageService {
@Autowired
public BaseProductionPartsUsageService(final Logger logger) {
this.logger = logger;
logger.info("BaseProductionPartsUsageService initialized");
}
protected Logger logger;
@Autowired
public void setStorageService(mStorageService storageService) {
this.storageService = storageService;
}
protected mStorageService storageService;
// calls several data service methods in method annotated with @Transactional
.
.
.
}
有时我们会遇到死锁,当我们在 SQLServer 分析器中检查详细信息时,我们可以看到由 READ_UNCOMMITED 注释的查询方法在分析器 xml 中是 READ_COMMITED。