av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

關于Java中的mysql時區問題詳解

瀏覽:73日期:2022-09-01 10:42:01

前言

話說工作十多年,mysql 還真沒用幾年。起初是外企銀行,無法直接接觸到 DB;后來一直從事架構方面,也多是解決問題為主。

這次搭建海外機房,圍繞時區大家做了一番討論。不說最終的結果是什么,期間有同事認為 DB 返回的是 UTC 時間。

這里簡單做個驗證,順便看下時區的問題到底是如何處理。

環境

openjdk version “1.8.0_242”mysql-connector-java “8.0.20”mysql “5.7” 時區 TZ=Europe/London

本地時區 GMT+8

創建個簡單的庫test及表user, 表結構如下:

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

插入一條測試數據:

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)

測試代碼:

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());}

執行結果:

2020-05-14T15:00

分析

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

關于Java中的mysql時區問題詳解

在 TCP 抓包結果中 #155 的查詢語句:

/* 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_packetAS max_allowed_packet, @@net_write_timeoutAS net_write_timeout, @@performance_schemaAS 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;

關于Java中的mysql時區問題詳解

服務端返回的 time_zone 為 BST。與本地時區的轉換,由 mysql 的 connector 自動完成。

進階

時區自動轉換

實現源碼:

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);}

如何確認服務端時區?

使用會話中的服務端時區進行服務端時區。會話初始化時會進行時區的確認,比如前面獲取的到BST。確認時區的邏輯在NativeProtocol#configureTimezone()中:

public void configureTimezone() { #從mysql的響應獲取 time_zone 和 system_time_zone 的設置 String configuredTimeZoneOnServer = this.serverSession.getServerVariable('time_zone'); if ('SYSTEM'.equalsIgnoreCase(configuredTimeZoneOnServer)) { configuredTimeZoneOnServer = this.serverSession.getServerVariable('system_time_zone'); } #從 jdbc url 參數 serverTimezone 獲取時區 String canonicalTimezone = getPropertySet().getStringProperty(PropertyKey.serverTimezone).getValue(); if (configuredTimeZoneOnServer != null) { //如果 jdbc url 中未通過 serverTimezone 指定時區。則從TimeZoneMapping.properties中獲取mysql 回傳的時區縮寫對應的標準時區,比如此處的 BST => Europe/London //會出現無法映射的情況,不如 CEST 無法映射到 => Europe/Berlin,可以指定自定義的 Properties 文件進行映射 // 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 中通過 serverTimezone 指定了時區,則優先使用該時區 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()); } }}

關于 serverTimezone 的官方說明

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

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

再次執行代碼:

2020-05-14T08:00

總結

到此這篇關于關于Java中mysql時區問題的文章就介紹到這了,更多相關Java中mysql時區問題內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 国产精品久久久久久久成人午夜 | 欧美性猛交xxxx黑人交 | 日本国产在线 | 国产精品久久久久久无人区 | 性色av蜜臀av浪潮av老女人 | 欧美一区二区三区免费 | 日韩欧美精品一区 | 国产黄a三级三级看三级 | 欧美三级又粗又硬 | 国产成人精品一区二区三区在线 | 欧美日韩亚洲一区二区三区 | 国产成人精品亚洲 | 九九热在线观看视频 | 亚洲天天干| 韩日欧美| 久操视频在线观看 | 成人免费毛片男人用品 | 日韩精品免费在线观看 | 国产一区二区在线免费 | 亚州av在线 | 黄色大片av | 久久婷婷网 | 伊人成人在线 | 亚洲黄色三级 | 成人综合网站 | 精品久久视频 | 天天射综合 | 在线观看免费毛片 | 成人午夜毛片 | 一级免费毛片 | 精品一区二区三区在线观看 | 国产com | 日韩中文视频 | 欧美精品影院 | 日韩精品综合 | 在线观看黄网站 | h片在线播放 | 午夜在线观看免费视频 | 综合网在线 | 91午夜精品亚洲一区二区三区 | 日韩中文字幕一区二区 |