MyBatis querying MySQL for datetime type data time difference 14 hours

Source: Internet
Author: User
Tags american time

Today using MyBatis query MySQL data, all the time is wrong, all the time than the database time more than 14 hours, considered a bit, the initial decision is the system time zone problem. Because the MySQL time zone setting defaults to the operating system time zone, looking at the next CentOS time zone, East 8 is not wrong, so it can be decided that the code is set up a wrong time zone.

Now start debugging MyBatis source code, When debugging the Createfromtimestamp method to the com.mysql.cj.jdbc.io.JdbcTimestampValueFactory of the Mysql-connector-java-6.0.4.jar package, the underlying driver for MySQL was found to be Database query time with a calender do type conversion, calender record contains the time zone is CST, and China's time zone Asia/shanghai just 14 hours.

So why does the MyBatis connection database use CST's American time? Continue to view source discovery

When MySQL connects to the database, it reads the system's time zone settings from MySQL, Debug Com.mysql.cj.mysqla.MysqlaSession.java Configuretimezone method found, this.getservervariable ("System_time_zone") The time zone setting that is read from the system is CST

At this point, it is clear that the time zone is incorrect for MySQL, log on to Linux, execute Mysql-uroot-p, and then Run command show variables like '%time_zone% ', and find that the System_time_zone item is CST.

Now, to troubleshoot the problem, modify the MySQL time zone settings.



Huvushun
Links: https://www.jianshu.com/p/ea7ef2d29940
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.

MyBatis querying MySQL for datetime type data time difference 14 hours

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.