【工作记录】JDBC连接MySQL,跨时区调查CST转Asia/Shangha
根據業(yè)務要求,不同的國家設置jvm參數,來確定當前時區(qū)。
// -Duser.timezone=Asia/Kolkata 印度加爾各答 GMT+05:30
// -Duser.timezone=Asia/Bangkok 泰國曼谷 GMT+07:00
// -Duser.timezone=Asia/Shangha 中國上海 GMT+08:00
由于各個國家上線項目不完全相同,部分功能在不同的項目有不同實現(xiàn)方式。數據同步時,發(fā)生了跨時區(qū)同步數據的情況。
例如:
在某個國家部署A服務,在當地時間執(zhí)行同步任務,可以同步到正確的業(yè)務數據,其中業(yè)務數據類型使用java.util.Date。
另一個國家部署B(yǎng)服務,同樣業(yè)務數據類型使用java.util.Date,在當地時間執(zhí)行同步任務,不可以同步到正確的業(yè)務數據,只好改寫為java.lang.String類型。
調查原因,發(fā)現(xiàn)B服務的jdbc的jar包版本太低,不支持自動時區(qū)轉換,升級版本為6.0.3,可以正確同步數據。
JdbcTimestampValueFactory
public Timestamp createFromTimestamp(int year, int month, int day, int hours, int minutes, int seconds, int nanos) {
if (year == 0 && month == 0 && day == 0) {
throw new DataReadException(Messages.getString("ResultSet.InvalidZeroDate"));
} else {
synchronized(this.cal) {
this.cal.set(year, month - 1, day, hours, minutes, seconds);
Timestamp ts = new Timestamp(this.cal.getTimeInMillis());
ts.setNanos(nanos);
return ts;
}
}
}
但同時發(fā)現(xiàn)A服務不需要指定jdbc.url中的serverTimezone=Asia/Shanghai。
跟蹤代碼MysqlaSession.configureTimezone(),在不指定serverTimezone時,跟蹤獲取數據庫服務器的時區(qū)的代碼,獲取的確實為CST,但在將轉換為對應時區(qū)時,A服務將CST轉為了Asia/Shanghai,但B服務仍然為CST=Asia/Kolkata。
MysqlaSession
public void configureTimezone() {
String configuredTimeZoneOnServer = this.getServerVariable("time_zone");
if ("SYSTEM".equalsIgnoreCase(configuredTimeZoneOnServer)) {
configuredTimeZoneOnServer = this.getServerVariable("system_time_zone");
}
String canonicalTimezone = (String)this.getPropertySet().getStringReadableProperty("serverTimezone").getValue();
if (configuredTimeZoneOnServer != null && (canonicalTimezone == null || StringUtils.isEmptyOrWhitespaceOnly(canonicalTimezone))) {
try {
canonicalTimezone = TimeUtil.getCanonicalTimezone(configuredTimeZoneOnServer, this.getExceptionInterceptor());
} catch (IllegalArgumentException var4) {
throw (WrongArgumentException)ExceptionFactory.createException(WrongArgumentException.class, var4.getMessage(), this.getExceptionInterceptor());
}
}
if (canonicalTimezone != null && canonicalTimezone.length() > 0) {
this.serverTimezoneTZ = TimeZone.getTimeZone(canonicalTimezone);
if (!canonicalTimezone.equalsIgnoreCase("GMT") && this.serverTimezoneTZ.getID().equals("GMT")) {
throw (WrongArgumentException)ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("Connection.9", new Object[]{canonicalTimezone}), this.getExceptionInterceptor());
}
}
this.defaultTimeZone = this.serverTimezoneTZ;
}
繼續(xù)跟進轉換代碼,在com.mysql.cj.jdbc.util.TimeUtil#loadTimeZoneMappings()中,會加載/com/mysql/cj/jdbc/util/TimeZoneMapping.properties文件,在A服務的資源文件中,有個TimeZoneMapping.properties,并且包含CST=Asia/Shanghai。Git代碼提交記錄備注為CST TimeZone point to Asia/Shanghai。
TimeUtil
private static void loadTimeZoneMappings(ExceptionInterceptor exceptionInterceptor) {
timeZoneMappings = new Properties();
try {
timeZoneMappings.load(TimeUtil.class.getResourceAsStream("/com/mysql/cj/jdbc/util/TimeZoneMapping.properties"));
} catch (IOException var5) {
throw ExceptionFactory.createException(Messages.getString("TimeUtil.LoadTimeZoneMappingError"), exceptionInterceptor);
}
String[] var1 = TimeZone.getAvailableIDs();
int var2 = var1.length;
for(int var3 = 0; var3 < var2; ++var3) {
String tz = var1[var3];
if (!timeZoneMappings.containsKey(tz)) {
timeZoneMappings.put(tz, tz);
}
}
}
``
重要參考鏈接:
- https://www.cnblogs.com/sogeisetsu/p/12324161.html
- https://blog.csdn.net/weixin_37015554/article/details/105198428
- https://blog.csdn.net/weixin_41917635/article/details/103719481
總結
以上是生活随笔為你收集整理的【工作记录】JDBC连接MySQL,跨时区调查CST转Asia/Shangha的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编程修养(二)
- 下一篇: golang微服务网关一:网络基础知识扫