Changing CURRENT_TIMESTAMP timezone in MySql

I have a (java) application server using America / Los_Angeles timezone but a database using UTC. I would like the dates in the database to reflect the appserver timezone (this is where the development team is, so it is useful to have all dates and times in their timezone). I am using Hibernate to map java.util.Date fields to MySQL TIMESTAMP and DATETIME fields. I have a create_date field (set by application) and update_date with this definition:

  `update_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

      

When I paste the app, it works fine: if it's 9pm in America / Los _Angeles, the dB shows 21:00 (although the connection timezone is UTC, which is strange but desirable). When I receive the item, create_date is still 21:00. Good so far. But when I do the update, CURRENT_TIMESTAMP comes in and shows 04:00 the next day. When the app fetches the item, it still says 04:00.

I've tried many combinations of useTimezone = true, serverTimezone = America / Los_Angeles, useGMTMillisForDatetimes = true and useLegacyDatetimeCode = false. I have also tried

  `update_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE 'CONVERT_TZ(CURRENT_TIMESTAMP,''+00:00'',''-07:00'')'

      

But the database hated it and I couldn't get it to work. I could just remove the ON UPDATE trigger, but then I will lose auditing if someone updates the db directly.

Strangely, even if I set serverTimezone = UTC, when I paste from the app, create_date still appears as 21:00 and not UTC. Anyway, there seems to be a timezone change on the app server side. My JPA mapping annotations for create_date

@Column(name='create_date', updatable=false)
@Temporal(TemporalType.TIMESTAMP)
Date createDate

      

Is there a way to get this to work correctly?

I am using ConnectorJ 5.1.29 and MySQL server 5.6.13.

+3


source to share





All Articles