Mysql 時間類型精度截取的bug


mysql-connector-java版本升級出現的一次問題。涉及到了時間精度的截取和四舍五入。

 

首先了解一點,timestamp,datetime如果不指定精度,默認的精度是秒。

 

當mysql-connector-java版本<=5.1.22時,db的客戶端會將Datetime,Timestamp秒以下的精度丟棄。版本>5.1.22后,秒以下的值將不會截斷

db的server端會對超出精度位數的數據進行四舍五入!!

 

舉個例子:在db建表時沒指定精度時,插入精確到毫秒級別的日期

如果使用mysql-connector-java版本<=5.1.22,在客戶端用'2018-04-02 23:59:59.999'插入日期,精度會在客戶端被截取到秒,插入db里是'2018-04-02 23:59:59'

如果升級版本,在db的客戶端用'2018-04-02 23:59:59.999'插入日期,精度在客戶端不會被截斷,db的server端會對超出精度位數的數據進行四舍五入,即插入db里是'2018-04-03 00:00:00 '

 

所以說mysql-connector-java版本升級就帶了時間與原本不一致的問題,結合具體業務邏輯上的使用,可能會造成不同大小的影響。

 

 

要想證實這個觀點,可以分兩步:

  • server端是否會四舍五入
  • 客戶端代碼不同版本對精度是否有不同的處理方式

 

來實際測一下server會不會四舍五入:

CREATE TABLE `time_test` (  
  `id` int(11) NOT NULL AUTO_INCREMENT ,  
  `create_time` timestamp NOT NULL DEFAULT '1971-01-01 00:00:00' ,  
  `end_time` timestamp NOT NULL DEFAULT '1971-01-01 00:00:00' ,  
  PRIMARY KEY (`id`)  
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;  
   
   
insert into time_test (create_time,end_time) values('2018-04-02 23:59:59','2018-04-02 23:59:59.999');  
   
   
select * from time_test;

  

看一下記錄:
+----+---------------------+---------------------+
| id | create_time         | end_time            |
+----+---------------------+---------------------+
|   2   2018 - 04 - 02  23 : 59 : 59     2018 - 04 - 03  00 : 00 : 00    |
+----+---------------------+---------------------+
 
 

可以看出db的server端果然會進行四舍五入。

 

再看一下mysql驅動里是怎么寫的,是否真的是截斷精度了。

 

Mysql對於時間精度的處理在com.mysql.jdbc.PreparedStatement#setTimestampInternal這個方法中

 

翻一下5.1.21的源碼看一下:

 

private void setTimestampInternal(int parameterIndex,  
      Timestamp x, Calendar targetCalendar,  
      TimeZone tz, boolean rollForward) throws SQLException {  
   synchronized (checkClosed()) {  
      if (x == null) {  
         setNull(parameterIndex, java.sql.Types.TIMESTAMP);  
      } else {  
         checkClosed();  
            
         if (!this.useLegacyDatetimeCode) {  
            newSetTimestampInternal(parameterIndex, x, targetCalendar);  
         } else {  
            String timestampString = null;  
   
            Calendar sessionCalendar = this.connection.getUseJDBCCompliantTimezoneShift() ?  
                  this.connection.getUtcCalendar() :  
                     getCalendarInstanceForSessionOrNew();  
                  
            synchronized (sessionCalendar) {  
               x = TimeUtil.changeTimezone(this.connection,  
                     sessionCalendar,  
                     targetCalendar,  
                     x, tz, this.connection  
                  .getServerTimezoneTZ(), rollForward);  
            }  
      
            if (this.connection.getUseSSPSCompatibleTimezoneShift()) {  
               doSSPSCompatibleTimezoneShift(parameterIndex, x, sessionCalendar);  
            } else {  
               synchronized (this) {  
                  if (this.tsdf == null) {  
                     //這里,截斷秒以下的精度  
                     this.tsdf = new SimpleDateFormat("''yyyy-MM-dd HH:mm:ss''", Locale.US); //$NON-NLS-1$  
                  }  
                     
                  timestampString = this.tsdf.format(x);  
                     
                  //這里永遠不會執行添加秒以下的精度  
                  if (false) { // not so long as Bug#50774 is around  
                     StringBuffer buf = new StringBuffer();  
                     buf.append(timestampString);  
                     int nanos = x.getNanos();  
                        
                     if (nanos != 0) {  
                        buf.append('.');  
                        buf.append(formatNanos(nanos));  
                     }  
                        
                     buf.append('\'');  
                  }  
                     
                  setInternal(parameterIndex, timestampString); // SimpleDateFormat is not  
                                                     // thread-safe  
               }  
            }  
         }  
            
         this.parameterTypes[parameterIndex - 1 + getParameterIndexOffset()] = Types.TIMESTAMP;  
      }  
   }  
} 

  

再看下5.1.32的實現:

private void setTimestampInternal(int parameterIndex,  
      Timestamp x, Calendar targetCalendar,  
      TimeZone tz, boolean rollForward) throws SQLException {  
   synchronized (checkClosed().getConnectionMutex()) {  
      if (x == null) {  
         setNull(parameterIndex, java.sql.Types.TIMESTAMP);  
      } else {  
         checkClosed();  
            
         if (!this.useLegacyDatetimeCode) {  
            newSetTimestampInternal(parameterIndex, x, targetCalendar);  
         } else {  
            Calendar sessionCalendar = this.connection.getUseJDBCCompliantTimezoneShift() ?  
                  this.connection.getUtcCalendar() :  
                     getCalendarInstanceForSessionOrNew();  
                  
            synchronized (sessionCalendar) {  
               x = TimeUtil.changeTimezone(this.connection,  
                     sessionCalendar,  
                     targetCalendar,  
                     x, tz, this.connection  
                  .getServerTimezoneTZ(), rollForward);  
            }  
      
            if (this.connection.getUseSSPSCompatibleTimezoneShift()) {  
               doSSPSCompatibleTimezoneShift(parameterIndex, x, sessionCalendar);  
            } else {  
               synchronized (this) {  
                  //同樣截斷精度  
                  if (this.tsdf == null) {  
                     this.tsdf = new SimpleDateFormat("''yyyy-MM-dd HH:mm:ss", Locale.US); //$NON-NLS-1$  
                  }  
                     
                  StringBuffer buf = new StringBuffer();  
                  buf.append(this.tsdf.format(x));  
   
                  //這里,如果server支持fractional seconds的話,就加上毫秒的精度  
                  if (this.serverSupportsFracSecs) {  
                     int nanos = x.getNanos();  
                        
                     if (nanos != 0) {  
                        buf.append('.');  
                        buf.append(TimeUtil.formatNanos(nanos, this.serverSupportsFracSecs, true));  
                     }  
                  }  
   
                  buf.append('\'');  
   
                  setInternal(parameterIndex, buf.toString()); // SimpleDateFormat is not  
                                                     // thread-safe  
               }  
            }  
         }  
            
         this.parameterTypes[parameterIndex - 1 + getParameterIndexOffset()] = Types.TIMESTAMP;  
      }  
   }  
}  

  

看來果然是個bug...看一下mysql官網的描述:參見bugFix第三條


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM