0

I get a strange exception tryining to connect to local mysql database. I am bulding spring mvc application.

This is how I configured DriverManagerDataSource:

@Bean
public DriverManagerDataSource dataSource(){
    DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
    driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
    driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/test");
    driverManagerDataSource.setUsername("****");
    driverManagerDataSource.setPassword("****");
    return driverManagerDataSource;
}

Those are log details that I get trying to connect:

    root cause

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: The server time zone value '�rodkowoeuropejski czas letni' 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.
    org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:394)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:474)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:484)
    com.packt.webstore.domain.repository.impl.CustomerRepositoryImpl.getAllCustomers(CustomerRepositoryImpl.java:35)
    com.packt.webstore.controller.HomeController.showCustomers(HomeController.java:36)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)

I figured out that it must be some problem with mysql-connector-java. I changed the version from 6.0.3 to 5.1.39. I just changed the version in my pom.xml file

 <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
    </dependency>
Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
Przemek
  • 647
  • 2
  • 8
  • 25
  • What version of the Oracle's JDBC you're using? – Julian Jul 28 '16 at 15:45
  • Well, to use it you simply add that `dependency` tag to your `pom.xml` file and Maven will do all the work for you (if you're using Maven) – Julian Jul 28 '16 at 15:47
  • It seems that the problem you're having might have been related to [this](http://stackoverflow.com/questions/2805907/setting-session-timezone-with-spring-jdbc-oracle) too. – Julian Jul 28 '16 at 15:48
  • And this has not be solved yet? – Przemek Jul 28 '16 at 15:51
  • It seems that it might have been a bug, so by upgrading the driver, the problem wouldn't happen anymore. – Julian Jul 28 '16 at 15:51
  • Has your problem been solved? If it has been, should i add an answer to close the topic? – Julian Jul 28 '16 at 15:53
  • So do you recomend my to leave the 5.1.39 version? I though that mamby some line of code could fix the issue with the newest version. Using the older version seems to be half-measure. – Przemek Jul 28 '16 at 15:54
  • If your work doesn't limit you to using certain versions of APIs you can pretty much always preffer to use the latest release. – Julian Jul 28 '16 at 15:56
  • I think you should check out this [question](http://stackoverflow.com/questions/5970835/setting-connection-timezone-with-spring-and-dbcp-and-mysql) as well – Julian Jul 28 '16 at 15:59
  • @Przemek would you consider accepting and/or give feedback/points to the other questions you posted; you know it's the way this site works... – Riccardo Cossu Jul 29 '16 at 13:17

2 Answers2

1

Replace your jdbc url by jdbc:mysql://localhost:3306/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC I hope it works

0

According to the stack errors.You should specify your time zone value.Add here is the same question.

Community
  • 1
  • 1
Demon Coldmist
  • 2,560
  • 2
  • 13
  • 21