简体   繁体   English

使用 mysql-connector-java jdbc 驱动程序 8.0.18 为 MySQL 8 服务器获取时间戳列的错误值

[英]Getting incorrect value for timestamp column with MySQL 8 server using mysql-connector-java jdbc driver 8.0.18

We have migrated our application from MySQL 5.1.6 to MySQL 8.0.16我们已将应用程序从 MySQL 5.1.6 迁移到 MySQL 8.0.16

While retrieving timestamp column from MySQL 8 server using mysql-connector-java jdbc driver (version 8.0.18, 8.0.19), we are getting incorrect value for the date value which is in daylight saving time (DST).使用 mysql-connector-java jdbc 驱动程序(版本 8.0.18、8.0.19)从 MySQL 8 服务器检索时间戳列时,我们得到的日期值不正确,该值处于夏令时 (DST)。

Both our server hosting java application & MySQL database are in EST.我们的服务器托管 Java 应用程序和 MySQL 数据库都在 EST 中。

Here, we have a column created_date having type datetime.在这里,我们有一个类型为 datetime 的created_date列。 It has value '2020-03-17 23:01:54' for a specific row.对于特定行,它的值为 '2020-03-17 23:01:54'。 While retrieving same data through JDBC as:通过 JDBC 检索相同的数据时:

Timestamp timestamp = resultSet.getTimestamp("created_date"); 

we are getting value as 2020-03-18 00:01:54.0 ie DST is getting applied.我们在 2020-03-18 00:01:54.0 获得价值,即正在应用 DST。

There is a known bug in MySQL: https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-known-issues-limitations.html MySQL 中存在一个已知错误: https : //dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-known-issues-limitations.html

It suggest to use add additional parameter in jdbc url serverTimezone=UTC.建议在 jdbc url serverTimezone=UTC 中使用添加附加参数。 But we can't use UTC because we want to use deployment specific timezone in our application/database.但是我们不能使用 UTC,因为我们想在我们的应用程序/数据库中使用特定于部署的时区。

Note: We were not facing this issue with MySQL 5.1.注意:我们在 MySQL 5.1 中没有遇到这个问题。

Additional update: While inserting data into database, If I use jdbc url with parameter serverTimezone=UTC, then while retrieving timestamp column I get correct value.附加更新:在将数据插入数据库时​​,如果我使用带有参数 serverTimezone=UTC 的 jdbc url,那么在检索时间戳列时我会得到正确的值。

So it seems with serverTimezone=UTC, JDBC driver is not applying DST while storing timestamp in database.因此,在 serverTimezone=UTC 的情况下,JDBC 驱动程序在将时间戳存储在数据库中时并未应用 DST。 So workaround suggested (ie serverTimezone=UTC) seems to be working.因此建议的解决方法(即 serverTimezone=UTC)似乎有效。

But If I insert data using Load Data Local file command using JDBC url containing serverTimezone=UTC ie但是,如果我使用包含 serverTimezone=UTC 的 JDBC url 使用 Load Data Local file 命令插入数据,即

PreparedStatement stmt = connection.prepareStatement("LOAD DATA LOCAL INFILE.....")

it seems serverTimezone=UTC flag is getting ignored & data is getting stored with DST applied timestamp value.似乎 serverTimezone=UTC 标志被忽略,并且数据正在使用 DST 应用的时间戳值存储。 So while retrieving timestamp, we are getting incorrect value.因此,在检索时间戳时,我们得到了不正确的值。

Appreciate any suggestions.感谢任何建议。

我遇到了同样的问题,并通过安装新版本的 Connector/J 8 驱动程序(8.0.20)解决了这个问题: https ://dev.mysql.com/doc/relnotes/connector-j/8.0/en/news- 8-0-20.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM