1
votes

I am completely new in Spring and fail to connect to my MySQL database by Spring Hibernate. I get

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

My applicationContext.xml

<context:annotation-config />
<context:component-scan base-package="Zoostore" />
<tx:annotation-driven />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/zoostore?characterEncoding=UTF-8"/>
    <property name="username" value="root"/>
    <property name="password" value="your_password"/>
</bean>


<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:dataSource-ref="dataSource"
      />

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="transactionManager"
      class="org.springframework.orm.jpa.JpaTransactionManager"
      p:entityManagerFactory-ref="entityManagerFactory"
      p:persistenceUnitName="BookJpaPersistenceUnit"/>

<tx:annotation-driven transaction-manager="transactionManager"/>

And that is part of my controller code where I try to store data to database and get it from it

@RequestMapping(value = "saveadvert", method = RequestMethod.POST)
public ModelAndView saveAdvert(Advert advert) {
    advert.setUserId(1);
    advertService.persist(advert);
    return new ModelAndView("test", "allAdverts", advertService.getAll());

This is the exception I get:

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)

I can provide the root causes if necessary

3
are you sure your mysql server is online? - dieter
yes, it's definitely online - FarizHuseynli

3 Answers

0
votes

Not sure which kind of error, I mention the conditions I had met.

  1. make sure you open mysql service, you can test connect it with IDE database function, like IDEA.
  2. take care of useSSL=false, sometimes it will be fatal.
  3. If your mysql-connector-java's version is above 8.*, that you might need to change the datasource-driver-name to com.mysql.cj.jdbc.Driver

Though spring will help you change.

  1. If you specify jpa with table's names, columns. Make sure you use the right grammar @Table(name='xxx') @Column(name= 'xxx')
  2. Take care of the mysql server's port.

In windows. netstat -ano |findstr: 3306

taskkill /PIDĀ enterPIDĀ /F

In Linux netstat -anp |grep 3306 kill -9 pid

  • Check gradle or maven dependencies
  • And here is where I get stuck.

In spring-boot, you can make the configuration set up automatically. But you need to make the files right in their places.

  • xx.yml need put in resources(In IDEA, you need to specify the package as resources package)
  • xxxApplication.class(The spring-boot-starter class should be out of other classes, or you should specify the other classes' path.
0
votes
  1. First check your Sql client Version.
  2. Goto Spring->ProjectName->WebContent->WEB-INF->lib and check you have the same mysql connector jar version as of Sql Client.
  3. Also change the same in Project->properties->Java-Build_path.
-1
votes

try this, it's works and solve your problem.

<beans xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-3.0.xsd       http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:annotation-config/><mvc:annotation-driven/><context:component-scan base-package="com.controller"/><bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><property name="basename" value="/WEB-INF/Messages"/><property name="cacheSeconds" value="3000"/></bean><tx:annotation-driven transaction-manager="transactionManager"/><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop><prop key="hibernate.show_sql">true</prop></props></property><property name="packagesToScan" value="com.controller"/></bean><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/demo1" p:username="root" p:password="chirag"/><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="10000000000"/></bean><bean id="viewResolver" class="org.springframework.web.servlet.view.Inter`enter code here`nalResourceViewResolver" p:prefix="/views/" p:suffix=".jsp"/></beans>