【问题标题】:Cannot connect to the MySQL database using Spring Hibernate无法使用 Spring Hibernate 连接到 MySQL 数据库
【发布时间】:2015-06-11 21:51:31
【问题描述】:

我是 Spring 的新手,无法通过 Spring Hibernate 连接到我的 MySQL 数据库。我明白了

请求处理失败;嵌套异常是: org.springframework.transaction.CannotCreateTransactionException:

无法打开 JPA EntityManager 进行事务处理;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException:无法打开 连接

我的 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"/>

这是我的控制器代码的一部分,我尝试将数据存储到数据库并从中获取数据

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

这是我得到的例外:

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)

如有必要,我可以提供根本原因

【问题讨论】:

  • 你确定你的mysql服务器在线吗?
  • 是的,肯定是在线的

标签: mysql hibernate spring-mvc


【解决方案1】:
  1. 首先检查您的 Sql 客户端版本。
  2. 转到 Spring->ProjectName->WebContent->WEB-INF->lib 并检查您是否有相同的 mysql Sql Client 的连接器 jar 版本。
  3. 在 Project->properties->Java-Build_path 中也进行相同的更改。

【讨论】:

    【解决方案2】:

    不确定是哪种错误,我提一下我遇到的条件。

    1. 确保你打开了mysql服务,你可以用IDE数据库功能测试连接它,比如IDEA。
    2. 小心useSSL=false,有时会致命。
    3. 如果你的mysql-connector-java的版本高于8.*,那你可能需要把datasource-driver-name改成com.mysql.cj.jdbc.Driver

    虽然春天会帮助你改变。

    1. 如果您使用表名、列指定 jpa。确保使用正确的语法 @Table(name='xxx') @Column(name= 'xxx')
    2. 注意mysql服务器的端口。

    在窗口中。 netstat -ano |findstr: 3306

    taskkill /PID enterPID /F

    在 Linux 中 netstat -anp |grep 3306 kill -9 pid

    • 检查 gradle 或 maven 依赖项
    • 这就是我卡住的地方。

    在 spring-boot 中,您可以自动设置配置。但是您需要将文件正确放置在它们的位置。

    • xx.yml需要放入资源(在IDEA中需要指定包为资源包)
    • xxxApplication.class(spring-boot-starter 类应该在其他类之外,或者你应该指定其他类的路径。

    【讨论】:

      【解决方案3】:

      试试这个,它可以解决你的问题。

      <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>
      

      【讨论】:

        猜你喜欢
        • 2020-09-03
        • 2018-10-09
        • 1970-01-01
        • 2019-02-26
        • 2023-04-04
        • 2017-01-13
        • 1970-01-01
        • 2011-06-20
        • 1970-01-01
        相关资源
        最近更新 更多