亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

H2 中的 DATETIME 值與從 Java/Kotlin 插入的 MySQL 數據庫之間的不匹配

H2 中的 DATETIME 值與從 Java/Kotlin 插入的 MySQL 數據庫之間的不匹配

當年話下 2022-09-07 17:35:39
如何使用Java Hibernate始終將正確的UTC日期時間值保存到H2和MySQL數據庫的DATETIME類型字段中?完整上下文:我在數據庫中有一個帶有DATETIME字段的表,我想在其中插入行:默認情況下(未給出值時)將存儲當前UTC時間或者,如果給定了 UTC 日期時間,則應在不進行其他時區轉換的情況下存儲該時間。它必須在本地H2數據庫以及Docker內部的本地mysql和外部AWS RDS MySQL實例上運行的問題。而且我很難在所有3個實例中正確保存日期時間。到目前為止,本地和aws mysql實例要么獲得正確的值,但本地H2獲得錯誤的值,要么相反,當本地H2獲得正確的值但MySQL實例獲得錯誤的值時。以下是我擁有的kotlin代碼的縮短片段。適用于 H2 但不適用于 Docker 和 AWS 中的 MySQL 的代碼:@Entitydata class SomeEntity(    val createdAt: LocalDateTime = LocalDateTime.now(Clock.systemUTC())    // If createdAt is not explicitly given when saving new entry in db, the default value will be used    // and H2 will get correct value of '2019-03-28 12:36:56',    // but it will be wrong for MySQL, it will get '2019-03-28 11:36:56')val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter)// In this case when createdAt is explicitly given when saving new entry in db,// H2 gets correct value '2012-11-30 16:13:21', // but MySQL DBs will get wrong value of '2012-11-30 17:13:21'適用于 Docker 和 AWS 中的 MySQL 但不適用于 H2 的代碼:@Entitydata class SomeEntity(    val createdAt: Date = Date()    // If createdAt is not explicitly given when saving new entry in db, the default value will be used    // and MySQL DBs will get correct value of '2019-03-28 12:36:56'    // but it will be wrong for H2 as it will get '2019-03-28 13:36:56' (my current local time instead of UTC))val dateTimeFormatter = SimpleDateFormat("yyyy-MM-dd H:mm:ss")dateTimeFormatter.timeZone = TimeZone.getTimeZone("UTC")createdAt = dateTimeFormatter.parse("2012-11-30 16:13:21")// In this case when createdAt is explicitly given when saving new entry in db,// MySQL DBs will get correct value '2012-11-30 16:13:21', // but H2 will get wrong value of '2012-11-30 17:13:21'運行于:Spring Boot 2.1.3,Hibernate Core 5.3.7,MySQL 8.0.13,H2 1.4.197我在網上和stackoverflow上看到了一堆問題,但不幸的是,沒有一個解決方案可以解決我的問題。
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

所以看起來修復是為JDBC連接設置UTC時區(而不是JVM):


spring.jpa.properties.hibernate.jdbc.time_zone=UTC

它依賴于 use 將值保留在 Java 端,并在 MySQL 和 H2 中具有 DATETIME 類型的字段。Instantcreated_at


縮短的結果 kotlin 代碼為:


@Entity

data class SomeEntity(

    val createdAt: Instant = Instant.now() // default created date is current UTC time

)


val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss")


createdAt = LocalDateTime.parse("2012-11-30 16:13:21", dateTimeFormatter).toInstant(ZoneOffset.UTC)

想法取自“Joop Eggen”的評論,這個和這篇文章。


獎金


我想如果你正在閱讀這篇文章,你可能還需要調試SQL查詢的幫助。


1. 要打印在 H2 上運行的 SQL 查詢,請添加和連接字符串(請參閱此處):TRACE_LEVEL_FILE=2TRACE_LEVEL_SYSTEM_OUT=2


spring.datasource.url=jdbc:h2:mem:dbname;TRACE_LEVEL_FILE=2;TRACE_LEVEL_SYSTEM_OUT=2;

2. 要啟用休眠日志:


spring.jpa.properties.hibernate.show_sql=true

spring.jpa.properties.hibernate.use_sql_comments=true

spring.jpa.properties.hibernate.format_sql=true

logging.level.org.hibernate.type=TRACE

3. 要在 MySQL 中啟用查詢日志(其中一種方法,請勿在生產數據庫上使用!


SET GLOBAL general_log = 'ON';

SET global log_output = 'table';

select * from mysql.general_log ORDER BY event_time DESC;


查看完整回答
反對 回復 2022-09-07
  • 1 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號