日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

【工作记录】JDBC连接MySQL,跨时区调查CST转Asia/Shangha

發(fā)布時間:2023/12/15 综合教程 37 生活家
生活随笔 收集整理的這篇文章主要介紹了 【工作记录】JDBC连接MySQL,跨时区调查CST转Asia/Shangha 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

根據業(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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。