0

I have Spring Boot Data JPA project running with UTC timezone. This project communicates with MSSQL database which runs with CST timezone.

Now I understand that if I want to instruct spring boot project to consider database timezone, I have below 2 options.

  1. Change default TimeZone of JVM to CST; which I do not want as I want to keep Spring boot application's timezone as UTC only.
  2. In application.properties file, set property spring.jpa.properties.hibernate.jdbc.time_zone=America/Chicago so that hibernate can convert date from UTC to CST whenever a record is inserted/updated/read from/to database.

Second option works great for me; however, I have a situation that underlying database could sooner be deployed on different datastore with another timezone. Hence I want to make the time zone property configurable (By configurable, I mean I will make this timezone as an environment variable, so that I no longer need spring boot app reboot).

Is there any way to set the property spring.jpa.properties.hibernate.jdbc.time_zone programmatically?

Vishal
  • 674
  • 1
  • 7
  • 20
  • Maybe [this](https://stackoverflow.com/questions/29072628/how-can-i-override-spring-boot-application-properties-programmatically) will be helpful. – SternK Aug 03 '21 at 09:35
  • @SternK let me try it out – Vishal Aug 03 '21 at 09:37
  • @SternK, above link looks okay if I want to override it once during application life cycle. Once my env variable is changed, how will it reflect as the property overload happens once only? – Vishal Aug 03 '21 at 09:46
  • 1
    Is [Externalized Configuration](https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html) something you're looking for? – Stefan Golubović Aug 03 '21 at 10:26
  • There are several ways to programatically set that property. I found at least two of them described [here](https://spring.io/blog/2020/04/23/spring-tips-configuration). I would favor the `contributeToTheEnvironment` method described, but that's a personal preference. – coladict Aug 05 '21 at 08:34

0 Answers0