File tree Expand file tree Collapse file tree 1 file changed +17
-6
lines changed
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit Expand file tree Collapse file tree 1 file changed +17
-6
lines changed Original file line number Diff line number Diff 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 ) {
You can’t perform that action at this time.
0 commit comments