Set default timezone H2 database

自闭症网瘾萝莉.ら 提交于 2019-11-29 04:08:15

Apparently you don't have a parameter on the connection but the database will use the timezone of the JVM where the driver is loaded so you can set -Duser.timezone=UTC. Note that you can't change the timezone after the driver has been loaded.

You can manipulate the JVM time zone, before interacting with the database:

TimeZone.setDefault(TimeZone.getTimeZone("UTC"))

Unfortunately, H2 does not support time zone per connection... yet.

You can't set the timezone for the database.

But, I'm not aware that H2 uses a timezone (at least not a current version of H2) for most operations.

So, what problem do you want to solve?

user666

H2 uses JVM timezone and it affects your Date calculation. If you are using it in Junits for example, you can set a certain timezone then re-put initial value when done. Example:

System.setProperty("user.timezone", "GMT-3");
TimeZone.setDefault(null);

I suppose you see such "misbehaviour" when reading types Date or DateTime.

Apparently H2 uses different time zone information during write and read. Providing a calendar with expected timezone returns the expected values for me:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(ZoneOffset.UTC))
resultSet.getDate("dateColumn", cal)

What helped me was to set timezone config for JDBC instead of JVM, which also seems more reasonable and cleaner way, as it affects only the database instead of the whole JVM:

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

My answer to the other question might help with additional info.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!