解决办法

serverTimezone=CTT

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/shys?serverTimezone=CTT&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false

spring.datasource.username=xxxxxx

spring.datasource.password=xxxxxx

spring.datasource.initialize=false

排错过程

mysql-connector-java-8.0.13.jar

当 JDBC 与 MySQL 开始建立连接时

com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer() throws SQLException {

...

this.session.getProtocol().initServerSession();

...

}

com.mysql.cj.protocol.a.NativeProtocol.initServerSession() {

configureTimezone();

...

}

com.mysql.cj.protocol.a.NativeProtocol.configureTimezone() {

String configuredTimeZoneOnServer = this.serverSession.getServerVariable("time_zone");

if ("SYSTEM".equalsIgnoreCase(configuredTimeZoneOnServer)) {

configuredTimeZoneOnServer = this.serverSession.getServerVariable("system_time_zone");

}

String canonicalTimezone = getPropertySet().getStringProperty(PropertyKey.serverTimezone).getValue();

if (configuredTimeZoneOnServer != null) {

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

}

}

}

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

}

}

this.serverSession.setDefaultTimeZone(this.serverSession.getServerTimeZone());

}

追踪代码可知,当 MySQL 的 time_zone 值为 SYSTEM 时,会取 system_time_zone 值作为协调时区。

重点在这里!若 String configuredTimeZoneOnServer 得到的是 CST 那么 Java 会误以为这是 CST -0500 ,因此 TimeZone.getTimeZone(canonicalTimezone) 会给出错误的时区信息。

如图所示,本机默认时区是 Asia/Shanghai +0800 ,误认为服务器时区为 CST -0500 ,实际上服务器是 CST +0800 。

com.mysql.cj.ClientPreparedQueryBindings.setTimestamp(int parameterIndex, Timestamp x, Calendar targetCalendar, int fractionalLength) {

if (x == null) {

setNull(parameterIndex);

} else {

x = (Timestamp) x.clone();

if (!this.session.getServerSession().getCapabilities().serverSupportsFracSecs()

|| !this.sendFractionalSeconds.getValue() && fractionalLength == 0) {

x = TimeUtil.truncateFractionalSeconds(x);

}

if (fractionalLength < 0) {

// default to 6 fractional positions

fractionalLength = 6;

}

x = TimeUtil.adjustTimestampNanosPrecision(x, fractionalLength, !this.session.getServerSession().isServerTruncatesFracSecs());

this.tsdf = TimeUtil.getSimpleDateFormat(this.tsdf, "''yyyy-MM-dd HH:mm:ss", targetCalendar,

targetCalendar != null ? null : this.session.getServerSession().getDefaultTimeZone());

StringBuffer buf = new StringBuffer();

buf.append(this.tsdf.format(x));

if (this.session.getServerSession().getCapabilities().serverSupportsFracSecs()) {

buf.append('.');

buf.append(TimeUtil.formatNanos(x.getNanos(), 6));

}

buf.append('\'');

setValue(parameterIndex, buf.toString(), MysqlType.TIMESTAMP);

}

}

原因

Timestamp 被转换为会话时区的时间字符串了。问题到此已然明晰:

JDBC 误认为会话时区在 CST-5

JBDC 把 Timestamp+0 转为 CST-5 的 String-5

MySQL 认为会话时区在 CST+8,将 String-5 转为 Timestamp-13

最终结果相差 13 个小时!如果处在冬令时还会相差 14 个小时!

mysql 时差查了13小时_Mysql 时间差了 14 或 13 小时 com.mysql.cj.jdbc.Driver相关推荐

  1. mysql印度时区_一次 JDBC 与 MySQL 因 “CST” 时区协商误解导致时间差了 14 或 13 小时的排错经历...

    CST 时区 名为 CST 的时区是一个很混乱的时区,有四种含义: 美国中部时间 Central Standard Time (USA) UTC-06:00 澳大利亚中部时间 Central Stan ...

  2. 一次 JDBC 与 MySQL 因 “CST” 时区协商误解导致时间差了 14 或 13 小时的排错经历...

    2019独角兽企业重金招聘Python工程师标准>>> CST 时区 名为 CST 的时区是一个很混乱的时区,有四种含义: 美国中部时间 Central Standard Time ...

  3. mysql cst_一次 JDBC 与 MySQL 因 “CST” 时区协商误解导致时间差了 14 或 13 小时的排错经历...

    摘要 名为 CST 的时区是一个很混乱的时区,在与 MySQL 协商会话时区时,Java 会误以为是 CST -0500,而非 CST +0800. CST 时区 名为 CST 的时区是一个很混乱的时 ...

  4. mysql system_time_zone cst_【转贴】一次 JDBC 与 MySQL 因 “CST” 时区协商误解导致时间差了 14 或 13 小时的排错经历...

    摘要 名为 CST 的时区是一个很混乱的时区,在与 MySQL 协商会话时区时,Java 会误以为是 CST -0500 ,而非 CST +0800 . CST 时区 名为 CST 的时区是一个很混乱 ...

  5. com.mysql.cj.jdbc.Driver和com.mysql.jdbc.Driver的区别

    今天写东西测试的时候发现一个问题,如下: application.yml中数据源是这样配置的: 第一反应就是记忆中连接mysql的驱动不都是com.mysql.jdbc.Driver吗?com.mys ...

  6. spring boot 使用 com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别

    2019独角兽企业重金招聘Python工程师标准>>> 今天集成spring boot 2.1.1构建web应用并且集成jdbc,发现默认用的8.0.13, <dependen ...

  7. mysql.cj.jdbc_com.mysql.jdbc.Driver和com.mysql.cj.jdbc.Driver的区别

    概述:com.mysql.jdbc.Driver是mysql-connector-java 5中的,而com.mysql.cj.jdbc.Driver是mysql-connector-java 6中的 ...

  8. com.mysql.cj.jdbc.driver maven_com.mysql.jdbc.Driver和com.mysql.cj.jdbc.Driver的区别

    概述:com.mysql.jdbc.Driver是mysql-connector-java 5中的,而com.mysql.cj.jdbc.Driver是mysql-connector-java 6中的 ...

  9. Springboot项目中com.mysql.cj.jdbc.Driver在yml文件中爆红的原因

    前几天搭建了一个框架,出现一个奇怪的问题,配置mysql文件时,com.mysql.cj.jdbc.Driver一直在爆红,我以为是版本太低了,就升级了高版本,但是还在爆红,最后我在网上查了半天,网上 ...

最新文章

  1. 数据库和Webapp安全
  2. Apache Shiro第1部分–基础
  3. _GUN_SOURCE宏
  4. 学习 ASP.NET MVC (第二回)实战篇
  5. 李彦宏说互联网思维已过时,AI可以根本上变革交通、城市、农业和医疗
  6. python局域网嗅探_Python_sniffer(网络嗅探器)
  7. 计算机导论国内外发展,计算机导论第一章计算机发展历程.ppt
  8. Oracle执行计划使用分析SQL执行效率
  9. 《区块链+》读书感想
  10. Android 替换应用内so文件避免每次都要重新打包的麻烦
  11. github官网访问太慢
  12. python爬虫实战--爬取猫眼专业版-实时票房
  13. PTA天天练(大于身高的平均值 )
  14. 实验:JS判断浏览器中英文版本
  15. android5.0协调布局CoordinatorLayout(第二篇CollapsingToolbarLayout效果实现原理讲解)原理
  16. 【蓝桥杯程序设计大赛感想】 一路艰辛 一路收获
  17. Shell脚本笔记(3)- 变量子串
  18. 根据地址获取HTTP返回的状态码
  19. codeforces 750A New Year and Hurry
  20. 模块5-6 冗余网络

热门文章

  1. idea无法搜索插件问题解决
  2. idea配置 Tomcat Deployment添加时没有Artifact...选择的解决方案
  3. Python中单个下划线“ _”变量的用途是什么?
  4. MySQL 8.0版本无法使用 node、Navicat等三方工具连接的问题
  5. HTTP协议类POST 和GET的区别
  6. JavaScript中的函数
  7. 新书问答:Company-Wide Agility
  8. 学习 spring-boot (一)
  9. C++ ODB 框架(未实践使用)
  10. Spring data redis应用示例