Hibernate with MySQL throws "WrongArgumentException: Malformed database URL, failed to parse the connection string near: useJDBCCompliantTimezoneShift, useLegacyDatetimeCode, serverTimezone"


#1

I am using hibernate with java based mysql configuration, maven and eclipse.
Here is my configurator:

 config.setProperties(new Properties() {
            {
                put("hibernate.connection.username", "username");
                put("hibernate.connection.password", "password");
                put("hibernate.connection.driver_class",
                        "com.mysql.jdbc.Driver");
                put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
                put("hibernate.connection.url",
                        "jdbc:mysql://localhost:3306/ifinances?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC");
            }
        });

 SessionFactory sessionFactory = 
                config.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build());
        
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        AccountType accountType = new AccountType();
        
        accountType.setName("Checking");
        accountType.setCreatedDate(new Date());
        accountType.setLastUpdatedDate(new Date());
        accountType.setCreatedBy("user");
        accountType.setLastUpdatedBy("user");
        
        session.save(accountType);
        session.beginTransaction().commit();
        session.close();
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
	at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:132)
	at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:118)
	at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
	at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
	at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
	at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
	at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
	at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
	at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
	at com.akamai.hibernate.Application.main(Application.java:38)
Caused by: java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.core.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC'.
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:526)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:505)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:479)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:489)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:72)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:124)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:224)
	at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
	... 13 more
Caused by: com.mysql.cj.core.exceptions.UnableToConnectException: Cannot load connection class because of underlying exception: com.mysql.cj.core.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC'.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
	at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93)
	... 15 more
Caused by: com.mysql.cj.core.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC'.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
	at com.mysql.cj.core.conf.url.ConnectionUrlParser.processKeyValuePattern(ConnectionUrlParser.java:511)
	at com.mysql.cj.core.conf.url.ConnectionUrlParser.parseQuerySection(ConnectionUrlParser.java:491)
	at com.mysql.cj.core.conf.url.ConnectionUrlParser.getProperties(ConnectionUrlParser.java:616)
	at com.mysql.cj.core.conf.url.ConnectionUrl.collectProperties(ConnectionUrl.java:307)
	at com.mysql.cj.core.conf.url.ConnectionUrl.<init>(ConnectionUrl.java:292)
	at com.mysql.cj.core.conf.url.SingleConnectionUrl.<init>(SingleConnectionUrl.java:38)
	at com.mysql.cj.core.conf.url.ConnectionUrl.getConnectionUrlInstance(ConnectionUrl.java:202)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:194)
	... 14 more

#2

Found the solution, This the connector issue updated to latest version and it started working.


#3

You either need to change the MySQL Connector/J Driver to 5.x or change the URL since MySQL 8.0 has removed these properties:

  • useJDBCCompliantTimezoneShift
  • useLegacyDatetimeCode