Skip to content

Commit

Permalink
修复多数据源导致的bug问题
Browse files Browse the repository at this point in the history
  • Loading branch information
duimba committed Aug 22, 2019
1 parent 61710e6 commit 0951cdd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ public interface TinyIdInfoDAO {
TinyIdInfo queryByBizType(String bizType);

/**
* 根据id、oldMaxId、version更新最新的maxId
* 根据id、oldMaxId、version、bizType更新最新的maxId
* @param id
* @param newMaxId
* @param oldMaxId
* @param version
* @param bizType
* @return
*/
int updateMaxId(Long id, Long newMaxId, Long oldMaxId, Long version);
int updateMaxId(Long id, Long newMaxId, Long oldMaxId, Long version, String bizType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public TinyIdInfo queryByBizType(String bizType) {
}

@Override
public int updateMaxId(Long id, Long newMaxId, Long oldMaxId, Long version) {
public int updateMaxId(Long id, Long newMaxId, Long oldMaxId, Long version, String bizType) {
String sql = "update tiny_id_info set max_id= ?," +
" update_time=now(), version=version+1" +
" where id=? and max_id=? and version=?";
return jdbcTemplate.update(sql, newMaxId, id, oldMaxId, version);
" where id=? and max_id=? and version=? and biz_type=?";
return jdbcTemplate.update(sql, newMaxId, id, oldMaxId, version, bizType);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -25,7 +26,14 @@ public class DbSegmentIdServiceImpl implements SegmentIdService {
@Autowired
private TinyIdInfoDAO tinyIdInfoDAO;

/**
* Transactional标记保证query和update使用的是同一连接
*
* @param bizType
* @return
*/
@Override
@Transactional
public SegmentId getNextSegmentId(String bizType) {
// 获取nextTinyId的时候,有可能存在version冲突,需要重试
for (int i = 0; i < Constants.RETRY; i++) {
Expand All @@ -35,7 +43,8 @@ public SegmentId getNextSegmentId(String bizType) {
}
Long newMaxId = tinyIdInfo.getMaxId() + tinyIdInfo.getStep();
Long oldMaxId = tinyIdInfo.getMaxId();
int row = tinyIdInfoDAO.updateMaxId(tinyIdInfo.getId(), newMaxId, oldMaxId, tinyIdInfo.getVersion());
int row = tinyIdInfoDAO.updateMaxId(tinyIdInfo.getId(), newMaxId, oldMaxId, tinyIdInfo.getVersion(),
tinyIdInfo.getBizType());
if (row == 1) {
tinyIdInfo.setMaxId(newMaxId);
SegmentId segmentId = convert(tinyIdInfo);
Expand Down

0 comments on commit 0951cdd

Please sign in to comment.