Skip to content

Commit 8f0c4c7

Browse files
committed
sequence 升级提示
1 parent 1826956 commit 8f0c4c7

File tree

1 file changed

+17
-6
lines changed
  • mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit

1 file changed

+17
-6
lines changed

mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/Sequence.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,27 @@ protected static long getDatacenterId(long maxDatacenterId) {
127127

128128
/**
129129
* 获取下一个ID
130-
*
131130
* @return
132131
*/
133132
public synchronized long nextId() {
134133
long timestamp = timeGen();
135-
if (timestamp < lastTimestamp) {
136-
throw new MybatisPlusException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
137-
lastTimestamp - timestamp));
134+
if (timestamp < lastTimestamp) {//闰秒
135+
long offset = lastTimestamp - timestamp;
136+
if (offset <= 5) {
137+
try {
138+
wait(offset << 1);
139+
timestamp = timeGen();
140+
if (timestamp < lastTimestamp) {
141+
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", offset));
142+
}
143+
} catch (Exception e) {
144+
throw new RuntimeException(e);
145+
}
146+
} else {
147+
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", offset));
148+
}
138149
}
150+
139151
if (lastTimestamp == timestamp) {
140152
sequence = (sequence + 1) & sequenceMask;
141153
if (sequence == 0) {
@@ -147,8 +159,7 @@ public synchronized long nextId() {
147159

148160
lastTimestamp = timestamp;
149161

150-
return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | (workerId << workerIdShift)
151-
| sequence;
162+
return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence;
152163
}
153164

154165
protected long tilNextMillis(long lastTimestamp) {

0 commit comments

Comments
 (0)