tinyid icon indicating copy to clipboard operation
tinyid copied to clipboard

Tinyid-Server查询db最新号段的事务使用方式存在疑问

Open max-24 opened this issue 3 years ago • 1 comments

DbSegmentIdServiceImpl类查询db最新号段 @Transactional(isolation = Isolation.READ_COMMITTED) public SegmentId getNextSegmentId(String bizType) { // 获取nextTinyId的时候,有可能存在version冲突,需要重试 for (int i = 0; i < Constants.RETRY; i++) { TinyIdInfo tinyIdInfo = tinyIdInfoDAO.queryByBizType(bizType); ...... int row = tinyIdInfoDAO.updateMaxId(tinyIdInfo.getId(), newMaxId, oldMaxId, tinyIdInfo.getVersion(), tinyIdInfo.getBizType()); ..... } throw new TinyIdSysException("get next segmentId conflict"); } 1、查询db获取最新号段的确需要做容错重试,重试的逻辑应该是在更上层service业务做的,这里service只做单一的更新号段逻辑。 2、如果在上层service做重试,这里查询和更新操作可以不放在同一个事务中,如果考虑到同一个事务使用同一个mysql连接成本更低,这里就不用使用读已提交的隔离级别了。

max-24 avatar Jan 18 '22 06:01 max-24

#10

hackeryutu avatar Jun 29 '22 08:06 hackeryutu