我在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)
如有必要,我可以提供根本原因
答案 0 :(得分:0)
不确定哪种错误,我提到了我遇到的情况。
useSSL=false
,有时会致命。虽然春天会帮助你改变。
@Table(name='xxx')
@Column(name= 'xxx')
在Windows中。
netstat -ano |findstr: 3306
taskkill / PID enterPID / F
在Linux
中netstat -anp |grep 3306
kill -9 pid
在spring-boot中,您可以自动设置配置。但是你需要在他们的位置使文件正确。
答案 1 :(得分:0)
答案 2 :(得分:-1)
试试这个,它可以解决你的问题。
<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>