title: Cannot connect to the MySQL database using Spring Hibernate
body: <p>I am completely new in Spring and fail to connect to my MySQL database by Spring Hibernate. I get </p>
  
  <blockquote>
    <p>Request processing failed; nested exception is :
    org.springframework.transaction.CannotCreateTransactionException: </p>
    
    <p>Could not open JPA EntityManager for transaction; nested exception is
    javax.persistence.PersistenceException:
    org.hibernate.exception.GenericJDBCException: Could not open
    connection</p>
  </blockquote>
  
  <p><strong>My applicationContext.xml</strong></p>
  
  
  
  <pre><code>&lt;context:annotation-config /&gt;
  &lt;context:component-scan base-package="Zoostore" /&gt;
  &lt;tx:annotation-driven /&gt;
  &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/zoostore?characterEncoding=UTF-8"/&gt;
      &lt;property name="username" value="root"/&gt;
      &lt;property name="password" value="your_password"/&gt;
  &lt;/bean&gt;
  
  
  &lt;bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:dataSource-ref="dataSource"
        /&gt;
  
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
  &lt;bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entityManagerFactory-ref="entityManagerFactory"
        p:persistenceUnitName="BookJpaPersistenceUnit"/&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>And that is part of my controller code where I try to store data to database and get it from it</p>
  
  <pre><code>@RequestMapping(value = "saveadvert", method = RequestMethod.POST)
  public ModelAndView saveAdvert(Advert advert) {
      advert.setUserId(1);
      advertService.persist(advert);
      return new ModelAndView("test", "allAdverts", advertService.getAll());
  </code></pre>
  
  <p><strong>This is the exception I get:</strong></p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
      org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  </code></pre>
  
  <p>I can provide the root causes if necessary </p>