Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with timezone with hibernate and MySQL

I am trying to configure hibernate(5.3.3 Final) with tomcat (8.5) and mysql (v.8.0.12). When I launch my HibernateTest.java (very simple code from tutorial, no problem here) with the hibernate.connection.url set as ‘jdbc:mysql://localhost:3306/sakila’ I am encountering the following error:

Caused by: java.sql.SQLException: The server time zone value 'Paris, Madrid 
(heure d?été)' is unrecognized or represents more than one time zone. You 
must configure either the server or JDBC driver (via the serverTimezone 
configuration property) to use a more specifc time zone value if you want to 
utilize time zone support.

MySQL is currently set on the ‘SYSTEM’ timezone for the global and the session (mysql> select @@global.time_zone, @@session.time_zone). And my system timezone is indeed Paris/Madrid. In my hibernate.cfg.xml file, when I write the connection url :

jdbc:mysql://localhost:3306/sakila?useJDBCCompliantTimezoneShift=true;useLegacyDatetimeCode=false;serverTimezone=UTC;

The error is :

com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near '=false;serverTimezone=UTC;'.

It is not the problem mentioned in the stackoverflow post ‘issues with mysql server timezone and jdbc connection’, because the ‘&’ is refused by eclipse, see screenshot attached of the hibernate.cfg.xml file :

[The reference to entity "useLegacyDatetimeCode" must end with the delimiter ';'] 1

It is not an invisible character between 'mysql:' and '//localhost' as mentioned in the stackoverflow post ‘Malformed database URL, failed to parse the main URL sections’. I’ve tried to work the problem around by setting via MySql Workbench the option for the local time (default-time-zone = '+02:00') which fits with the summer time for Madrid/Paris (my case here). It doesn’t change a thing. Any idea? Do I have to configure it somewhere else? Thank you for your help, I've been on this one for 3 days now, without success.

like image 669
Codizon Avatar asked Aug 04 '18 15:08

Codizon


People also ask

Can we use hibernate with MySQL?

You will have to make sure that you have testdb database available in your MySQL database and you have a user test available to access the database. The XML configuration file must conform to the Hibernate 3 Configuration DTD, which is available at http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd.

How do I change timezone to UTC in MySQL?

Option 2: Edit the MySQL Configuration File Scroll down to the [mysqld] section, and find the default-time-zone = "+00:00" line. Change the +00:00 value to the GMT value for the time zone you want. Save the file and exit. In the example below we set the MySQL Server time zone to +08:00 (GMT +8).

How do I get current time zone in MySQL?

The following is the syntax to get the current time zone of MySQL. mysql> SELECT @@global. time_zone, @@session.

How do I set timezone in JDBC?

To set the timezone for a given JDBC connection, navigate to the Advanced tab and select the timezone from the dropdown menu. By default, UTC is selected.


2 Answers

I've finally came across a solution. As it looked that neither ';' nor '&' would do the trick to add more than one parameter, I took out all the parameters, and tried only one parameter :

jdbc:mysql://localhost:3306/sakila?serverTimezone=UTC

And it did the trick, I no longer have problems with this.

like image 93
Codizon Avatar answered Sep 17 '22 19:09

Codizon


You need to escape the &:

jdbc:mysql://localhost:3306/sakila?useSSL=false&serverTimezone=UTC

See more here: https://docs.jboss.org/exojcr/1.12.13-GA/developer/en-US/html/ch-db-configuration-hibernate.html

like image 23
user10408448_vova Avatar answered Sep 21 '22 19:09

user10408448_vova