欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于Java中的mysql時(shí)區(qū)問(wèn)題詳解

 更新時(shí)間:2020年05月22日 08:51:13   作者:AddoZhang  
這篇文章主要給大家介紹了關(guān)于Java中mysql時(shí)區(qū)問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

話說(shuō)工作十多年,mysql 還真沒(méi)用幾年。起初是外企銀行,無(wú)法直接接觸到 DB;后來(lái)一直從事架構(gòu)方面,也多是解決問(wèn)題為主。

這次搭建海外機(jī)房,圍繞時(shí)區(qū)大家做了一番討論。不說(shuō)最終的結(jié)果是什么,期間有同事認(rèn)為 DB 返回的是 UTC 時(shí)間。

這里簡(jiǎn)單做個(gè)驗(yàn)證,順便看下時(shí)區(qū)的問(wèn)題到底是如何處理。

環(huán)境

openjdk version “1.8.0_242”
mysql-connector-java “8.0.20”
mysql “5.7” 時(shí)區(qū) TZ=Europe/London

本地時(shí)區(qū) GMT+8

創(chuàng)建個(gè)簡(jiǎn)單的庫(kù)test及表user, 表結(jié)構(gòu)如下:

CREATE TABLE `user` (
 `name` varchar(50) NOT NULL,
 `birth_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1

插入一條測(cè)試數(shù)據(jù):

mysql> insert into `user`
  -> values ('Tom', time('2020-05-15 08:00:00'));
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+------+---------------------+
| name | birth_date     |
+------+---------------------+
| Tom | 2020-05-14 08:00:00 |
+------+---------------------+
1 row in set (0.00 sec)

測(cè)試代碼:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root", "root");
Statement stmt = conn.createStatement();
stmt.execute("select * from user where name = 'Tom'");
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
  Timestamp timestamp = rs.getTimestamp("birth_date");
  System.out.println(timestamp.toLocalDateTime().toString());
}

執(zhí)行結(jié)果:

2020-05-14T15:00

分析

程序的執(zhí)行過(guò)程同時(shí)用 wireshark 抓了包。可以看到一次查詢,做了這么多次的交互(包含了會(huì)話初始化)。這里可以看到 #177 的交互返回查詢的結(jié)果:Tom 2020-05-14 08:00:00,與 DB 中的數(shù)據(jù)相符??梢?jiàn),返回的并不是 UTC 時(shí)間。

在 TCP 抓包結(jié)果中 #155 的查詢語(yǔ)句:

/* mysql-connector-java-8.0.20 (Revision: afc0a13cd3c5a0bf57eaa809ee0ee6df1fd5ac9b) */
SELECT @@session.auto_increment_increment AS auto_increment_increment,
    @@character_set_client       AS character_set_client,
    @@character_set_connection     AS character_set_connection,
    @@character_set_results      AS character_set_results,
    @@character_set_server       AS character_set_server,
    @@collation_server         AS collation_server,
    @@collation_connection       AS collation_connection,
    @@init_connect           AS init_connect,
    @@interactive_timeout       AS interactive_timeout,
    @@license             AS license,
    @@lower_case_table_names      AS lower_case_table_names,
    @@max_allowed_packet        AS max_allowed_packet,
    @@net_write_timeout        AS net_write_timeout,
    @@performance_schema        AS performance_schema,
    @@query_cache_size         AS query_cache_size,
    @@query_cache_type         AS query_cache_type,
    @@sql_mode             AS sql_mode,
    @@system_time_zone         AS system_time_zone,
    @@time_zone            AS time_zone,
    @@transaction_isolation      AS transaction_isolation,
    @@wait_timeout           AS wait_timeout;

服務(wù)端返回的 time_zone 為 BST。與本地時(shí)區(qū)的轉(zhuǎn)換,由 mysql 的 connector 自動(dòng)完成。

進(jìn)階

時(shí)區(qū)自動(dòng)轉(zhuǎn)換

實(shí)現(xiàn)源碼:

ResultSetImpl源碼

this.defaultTimestampValueFactory = new SqlTimestampValueFactory(pset, null, this.session.getServerSession().getServerTimeZone());@Overridepublic Timestamp getTimestamp(int columnIndex) throws SQLException {
  checkRowPos();
  checkColumnBounds(columnIndex);  return this.thisRow.getValue(columnIndex - 1, this.defaultTimestampValueFactory);
}

如何確認(rèn)服務(wù)端時(shí)區(qū)?

使用會(huì)話中的服務(wù)端時(shí)區(qū)進(jìn)行服務(wù)端時(shí)區(qū)。會(huì)話初始化時(shí)會(huì)進(jìn)行時(shí)區(qū)的確認(rèn),比如前面獲取的到BST。確認(rèn)時(shí)區(qū)的邏輯在NativeProtocol#configureTimezone()中:

public void configureTimezone() {
  #從mysql的響應(yīng)獲取 time_zone 和 system_time_zone 的設(shè)置
  String configuredTimeZoneOnServer = this.serverSession.getServerVariable("time_zone");

  if ("SYSTEM".equalsIgnoreCase(configuredTimeZoneOnServer)) {
    configuredTimeZoneOnServer = this.serverSession.getServerVariable("system_time_zone");
  }

  #從 jdbc url 參數(shù) serverTimezone 獲取時(shí)區(qū)
  String canonicalTimezone = getPropertySet().getStringProperty(PropertyKey.serverTimezone).getValue();

  if (configuredTimeZoneOnServer != null) {
    //如果 jdbc url 中未通過(guò) serverTimezone 指定時(shí)區(qū)。則從TimeZoneMapping.properties中獲取mysql 回傳的時(shí)區(qū)縮寫(xiě)對(duì)應(yīng)的標(biāo)準(zhǔn)時(shí)區(qū),比如此處的 BST => Europe/London
    //會(huì)出現(xiàn)無(wú)法映射的情況,不如 CEST 無(wú)法映射到 => Europe/Berlin,可以指定自定義的 Properties 文件進(jìn)行映射
    // user can override this with driver properties, so don't detect if that's the case
    if (canonicalTimezone == null || StringUtils.isEmptyOrWhitespaceOnly(canonicalTimezone)) {
      try {
        canonicalTimezone = TimeUtil.getCanonicalTimezone(configuredTimeZoneOnServer, getExceptionInterceptor());
      } catch (IllegalArgumentException iae) {
        throw ExceptionFactory.createException(WrongArgumentException.class, iae.getMessage(), getExceptionInterceptor());
      }
    }
  }
  
  //如果 jdbc url 中通過(guò) serverTimezone 指定了時(shí)區(qū),則優(yōu)先使用該時(shí)區(qū)
  if (canonicalTimezone != null && canonicalTimezone.length() > 0) {
    this.serverSession.setServerTimeZone(TimeZone.getTimeZone(canonicalTimezone));

    //
    // The Calendar class has the behavior of mapping unknown timezones to 'GMT' instead of throwing an exception, so we must check for this...
    //
    if (!canonicalTimezone.equalsIgnoreCase("GMT") && this.serverSession.getServerTimeZone().getID().equals("GMT")) {
      throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("Connection.9", new Object[] { canonicalTimezone }),
          getExceptionInterceptor());
    }
  }

}

關(guān)于 serverTimezone 的官方說(shuō)明

Override detection/mapping of time zone. Used when time zone from server doesn't map to Java time zone

修改一下 jdbc url,通過(guò)serverTimezone指定時(shí)區(qū)為 GMT+8:jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=false

再次執(zhí)行代碼:

2020-05-14T08:00

總結(jié)

到此這篇關(guān)于關(guān)于Java中mysql時(shí)區(qū)問(wèn)題的文章就介紹到這了,更多相關(guān)Java中mysql時(shí)區(qū)問(wèn)題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java編程中10個(gè)最佳的異常處理技巧

    Java編程中10個(gè)最佳的異常處理技巧

    這篇文章主要介紹了Java編程中10個(gè)最佳的異常處理技巧,在本文中,將討論Java異常處理最佳實(shí)踐,這些Java最佳實(shí)踐遵循標(biāo)準(zhǔn)的JDK庫(kù),和幾個(gè)處理錯(cuò)誤和異常的開(kāi)源代碼,這還是一個(gè)提供給java程序員編寫(xiě)健壯代碼的便利手冊(cè),需要的朋友可以參考下
    2015-01-01
  • Java 類(lèi)動(dòng)態(tài)添加屬性字段的操作

    Java 類(lèi)動(dòng)態(tài)添加屬性字段的操作

    這篇文章主要介紹了Java 類(lèi)動(dòng)態(tài)添加屬性字段的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • 深入理解Java設(shè)計(jì)模式之職責(zé)鏈模式

    深入理解Java設(shè)計(jì)模式之職責(zé)鏈模式

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解
    2021-11-11
  • SpringCloud?GateWay網(wǎng)關(guān)示例代碼詳解

    SpringCloud?GateWay網(wǎng)關(guān)示例代碼詳解

    這篇文章主要介紹了SpringCloud?GateWay網(wǎng)關(guān),Spring?cloud?Gateway的功能很多很強(qiáng)大,文中提到了Spring?Cloud?Gateway中幾個(gè)重要的概念,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-04-04
  • 使用kotlin編寫(xiě)spring cloud微服務(wù)的過(guò)程

    使用kotlin編寫(xiě)spring cloud微服務(wù)的過(guò)程

    這篇文章主要介紹了使用kotlin編寫(xiě)spring cloud微服務(wù)的相關(guān)知識(shí),本文給大家提到配置文件的操作代碼,給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • 劍指Offer之Java算法習(xí)題精講鏈表與二叉樹(shù)專(zhuān)項(xiàng)訓(xùn)練

    劍指Offer之Java算法習(xí)題精講鏈表與二叉樹(shù)專(zhuān)項(xiàng)訓(xùn)練

    跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • 與近日火爆的ChatGPT聊Elasticsearch源碼

    與近日火爆的ChatGPT聊Elasticsearch源碼

    這篇文章主要為大家分享了與近日火爆的ChatGPT聊Elasticsearch源碼的話題內(nèi)容,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Spring詳解使用注解開(kāi)發(fā)流程

    Spring詳解使用注解開(kāi)發(fā)流程

    這篇文章主要為大家詳細(xì)介紹了Spring如何使用注解開(kāi)發(fā),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-05-05
  • Spring切入點(diǎn)表達(dá)式配置過(guò)程圖解

    Spring切入點(diǎn)表達(dá)式配置過(guò)程圖解

    這篇文章主要介紹了Spring切入點(diǎn)表達(dá)式配置過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯(cuò)問(wèn)題

    MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯(cuò)問(wèn)題

    這篇文章主要介紹了MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評(píng)論