Skip to content

Commit

Permalink
Merge pull request #18 from lookingatstarts/fix-transaction-isolation…
Browse files Browse the repository at this point in the history
…-bug

修复bug: 事务的隔离级别不对,从DEFAULT(mysql的REPEATABLE_READ)改成READ_COMMITTED
  • Loading branch information
duimba authored Jun 13, 2020
2 parents 3b51faa + 0de6f1d commit 49976d3
Showing 1 changed file with 6 additions and 1 deletion.
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.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -28,12 +29,16 @@ public class DbSegmentIdServiceImpl implements SegmentIdService {

/**
* Transactional标记保证query和update使用的是同一连接
* 事务隔离级别应该为READ_COMMITTED,Spring默认是DEFAULT(取决于底层使用的数据库,mysql的默认隔离级别为REPEATABLE_READ)
* <p>
* 如果是REPEATABLE_READ,那么在本次事务中循环调用tinyIdInfoDAO.queryByBizType(bizType)获取的结果是没有变化的,也就是查询不到别的事务提交的内容
* 所以多次调用tinyIdInfoDAO.updateMaxId也就不会成功
*
* @param bizType
* @return
*/
@Override
@Transactional
@Transactional(isolation = Isolation.READ_COMMITTED)
public SegmentId getNextSegmentId(String bizType) {
// 获取nextTinyId的时候,有可能存在version冲突,需要重试
for (int i = 0; i < Constants.RETRY; i++) {
Expand Down

0 comments on commit 49976d3

Please sign in to comment.