昨天,生产环境的Timestamp类型字段数据保存报错:
[2019-04-03 10:13:14.570 Thread-2025 ERROR] o.h.e.jdbc.spi.SqlExceptionHelper - Data truncation: Incorrect datetime value: '2090-01-06 16:00:01.294' for column 'LAST_ONLINE_TIME' at row 1
[2019-04-03 10:13:14.571 Thread-2025 ERROR] o.h.i.ExceptionMapperStandardImpl - HHH000346: Error during managed flush [org.hibernate.exception.DataException: could not execute statement]
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:300)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
Caused by: org.hibernate.exception.DataException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:52)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:104)
项目使用JPA操作数据库,看错误提示应该是数据库层面提示的问题,不是代码的锅。定睛一看,这"2090-01-06 16:00:01.294"时间格式也没啥问题,那是为啥保存报错说值不对呢?
算了,问问度娘吧,直接把异常po上去。
以下为前几页搜索结果:
1.数据库驱动jar包版本不对,mysql-connector-java版本要与数据库版本一致,好像此情况较多。
我感觉我碰到应该不是这个问题,版本不一致早就有问题了,不应该突然蹦出个字段赋值不正确吧。看了下MySQL的版本为:5.6(查看方式:SELECT VERSION();),我使用的mysql-connector-java版本是8.0.13,对照数据库驱动各版本官网说明,没毛病,排除该情况。
2. 建议重装MySQL,什么?你们是魔鬼吗?动不动就要重装?我选择Go die…
em…,搜索姿势不对结果就是这样咯,能把你看的一脸懵逼。
突然想起来是不是Timestamp类型字段赋值有限制范围啊,去官网(MySQL官网时间类型说明)一搜,果不其然:
Timestamp类型的取值范围是’1970-01-01 00:00:01’ 到 ‘2038-01-19 03:14:07’,很明显我要插入的时间值为’2090-01-06 16:00:01.294’是不符合规定的取值范围的。
根据官网描述:
A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision.
DATETIME和TIMESTAMP类型都支持高达微秒级的精度,其实我只需要到秒就行,那我把表里的字段类由’timestamp’改为’datetime’吧。再跑一遍,OK,问题解决。
注意事项:TIMESTAMP类型有取值范围限制的,慎用