others - java第一次登录:HTTP状态 500请求处理失败;嵌套异常为 org.springframework.transaction.CannotCreateTransactionException

我已经用spring,Hibernate和MySQL制作了一个Java web webapp ,它运行正常,但是,在第一次尝试连接时,我有以下错误:


HTTP Status 500 - Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed:



type Exception report



message Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed:



description The server encountered an internal error that prevented it from fulfilling this request.



exception



org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed: 


 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)


 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)


 javax.servlet.http.HttpServlet.service(HttpServlet.java:641)


 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause



org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed: 


 org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596)


 org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)


 org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:317)


 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)


 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)


 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)


 $Proxy31.getTodosUsuariosList(Unknown Source)


 com.companyname.springapp.service.SimpleAnonimoManager.identificarse(SimpleAnonimoManager.java:21)


 com.companyname.springapp.web.IdentificarseFormController.onSubmit(IdentificarseFormController.java:40)


 sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source)


 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


 java.lang.reflect.Method.invoke(Method.java:597)


 org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:174)


 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)


 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)


 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)


 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)


 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)


 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)


 javax.servlet.http.HttpServlet.service(HttpServlet.java:641)


 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause



org.hibernate.TransactionException: JDBC begin failed: 


 org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:96)


 org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463)


 org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)


 org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)


 org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:317)


 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)


 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)


 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)


 $Proxy31.getTodosUsuariosList(Unknown Source)


 com.companyname.springapp.service.SimpleAnonimoManager.identificarse(SimpleAnonimoManager.java:21)


 com.companyname.springapp.web.IdentificarseFormController.onSubmit(IdentificarseFormController.java:40)


 sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source)


 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


 java.lang.reflect.Method.invoke(Method.java:597)


 org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:174)


 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)


 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)


 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)


 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)


 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)


 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)


 javax.servlet.http.HttpServlet.service(HttpServlet.java:641)


 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause



com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure



The last packet successfully received from the server was 4,665,488 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.


 sun.reflect.GeneratedConstructorAccessor139.newInstance(Unknown Source)


 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)


 java.lang.reflect.Constructor.newInstance(Constructor.java:513)


 com.mysql.jdbc.Util.handleNewInstance(Util.java:411)


 com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)


 com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3670)


 com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3559)


 com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4110)


 com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)


 com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)


 com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809)


 com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5333)


 org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:331)


 org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setAutoCommit(PoolingDataSource.java:317)


 org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:91)


 org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463)


 org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)


 org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)


 org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:317)


 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)


 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)


 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)


 $Proxy31.getTodosUsuariosList(Unknown Source)


 com.companyname.springapp.service.SimpleAnonimoManager.identificarse(SimpleAnonimoManager.java:21)


 com.companyname.springapp.web.IdentificarseFormController.onSubmit(IdentificarseFormController.java:40)


 sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source)


 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


 java.lang.reflect.Method.invoke(Method.java:597)


 org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:174)


 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)


 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)


 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)


 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)


 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)


 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)


 javax.servlet.http.HttpServlet.service(HttpServlet.java:641)


 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause



java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.


 com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3119)


 com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3570)


 com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3559)


 com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4110)


 com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)


 com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)


 com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809)


 com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5333)


 org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:331)


 org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setAutoCommit(PoolingDataSource.java:317)


 org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:91)


 org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463)


 org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)


 org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)


 org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:317)


 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)


 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)


 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)


 $Proxy31.getTodosUsuariosList(Unknown Source)


 com.companyname.springapp.service.SimpleAnonimoManager.identificarse(SimpleAnonimoManager.java:21)


 com.companyname.springapp.web.IdentificarseFormController.onSubmit(IdentificarseFormController.java:40)


 sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source)


 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


 java.lang.reflect.Method.invoke(Method.java:597)


 org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:174)


 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)


 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)


 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)


 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)


 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)


 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)


 javax.servlet.http.HttpServlet.service(HttpServlet.java:641)


 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.32 logs.



Apache Tomcat/7.0.32



它只在第一次尝试时发生,重新加载页面或尝试再次识别时,它运行正常。

我在互联网上搜索许多可能的解决方案,我尝试了一些,但是,它还没有运行,我尝试了一个解决方案,当我上传它时,它运行得很好,但是,现在错误又出现了。

这些是我的配置文件:

jdbc.properties


hibernate.generate_statistics = true


hibernate.show_sql = true



jdbc.driverClassName=com.mysql.jdbc.Driver


jdbc.url=jdbc:mysql://localhost:3306/database


jdbc.username=user


jdbc.password=pass


hibernate.dialect=org.hibernate.dialect.MySQLDialect


hibernate.format_sql=true


hibernate.hbm2ddl.auto=update


hibernate.current_session_context_class=thread



hibernate.cfg.xml


<?xml version='1.0' encoding='utf-8'?>


<!DOCTYPE hibernate-configuration PUBLIC


 "-//Hibernate/Hibernate Configuration DTD//EN"


 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">



<hibernate-configuration>


 <session-factory>


 <mapping class="com.companyname.springapp.domain.Contrato" />


 <mapping class="com.companyname.springapp.domain.ContratoDraftInicial" />


 <mapping class="com.companyname.springapp.domain.Derecho" />


 <mapping class="com.companyname.springapp.domain.Equipo" />


 <mapping class="com.companyname.springapp.domain.EstadisticaEquipo" />


 <mapping class="com.companyname.springapp.domain.EstadisticaJugador" />


 <mapping class="com.companyname.springapp.domain.Jugador" />


 <mapping class="com.companyname.springapp.domain.Usuario" />


 </session-factory>



</hibernate-configuration>



app-config.xml


<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"


 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


 xmlns:mvc="http://www.springframework.org/schema/mvc"


 xmlns:context="http://www.springframework.org/schema/context"


 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd


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



 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">


 <property name="basename" value="messages"/>


 </bean>



 <!-- Scans the classpath of this application for @Components to deploy as beans -->


 <context:component-scan base-package="com.companyname.springapp.web" />



 <!-- Configures the @Controller programming model -->


 <mvc:annotation-driven/>



 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">


 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>


 <property name="prefix" value="/jsp/"></property>


 <property name="suffix" value=".jsp"></property> 


 </bean>



</beans>



applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"


 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


 xmlns:context="http://www.springframework.org/schema/context"


 xmlns:p="http://www.springframework.org/schema/p"


 xmlns:tx="http://www.springframework.org/schema/tx"


 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">



 <!-- holding properties for database connectivity /-->


 <context:property-placeholder location="classpath:jdbc.properties"/>



 <!-- enabling annotation driven configuration /-->


 <context:annotation-config/>



 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">


 <property name="driverClassName" value="${jdbc.driverClassName}"/>


 <property name="url" value="${jdbc.url}"/>


 <property name="username" value="${jdbc.username}"/>


 <property name="password" value="${jdbc.password}"/>


 </bean>



 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">


 <property name="dataSource" ref="dataSource"/>


 <property name="configLocation">


 <value>classpath:hibernate.cfg.xml</value>


 </property>


 <property name="configurationClass">


 <value>org.hibernate.cfg.AnnotationConfiguration</value>


 </property>


 </bean>



 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">


 <property name="sessionFactory" ref="sessionFactory"/>


 </bean>



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



 <!-- Scans the classpath of this application for @Components to deploy as beans -->


 <context:component-scan base-package="com.companyname.springapp.repository" />


 <context:component-scan base-package="com.companyname.springapp.service" />



</beans>



web.xml


<web-app version="2.4"


 xmlns="http://java.sun.com/xml/ns/j2ee"


 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<!-- 


<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"


 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


 -->



 <context-param>


 <param-name>contextConfigLocation</param-name>


 <param-value>/WEB-INF/spring/applicationContext.xml</param-value>


 </context-param>



 <listener>


 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>


 </listener>



 <display-name>Springapp</display-name>



 <servlet>


 <servlet-name>springapp</servlet-name>


 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>


 <init-param>


 <param-name>contextConfigLocation</param-name>


 <param-value>/WEB-INF/spring/app-config.xml</param-value>


 </init-param>


 <load-on-startup>1</load-on-startup>


 </servlet>



 <servlet-mapping>


 <servlet-name>springapp</servlet-name>


 <url-pattern>*.htm</url-pattern>


 </servlet-mapping>



</web-app>



要调用数据库,我执行这个(我给大家举个例子): EquipoDaoImp.java


@Repository(value = "equipoDao")


public class EquipoDaoImp implements EquipoDao {



 private final SessionFactory sessionFactory;



 @Autowired


 public EquipoDaoImp(SessionFactory sessionFactory) {


 this.sessionFactory = sessionFactory;


 }



 @Override


 @Transactional(readOnly = true)


 public Integer getIdEquipo(String nombreUsuario) {


 return (Integer) this.sessionFactory.getCurrentSession()


 .createQuery("from Equipo e order by e.id")


 .uniqueResult();


 }



}



为什么会发生这种情况?

时间:

似乎MySQL或防火墙正在杀掉你的jdbc连接池中长时间闲置的非活动连接:


com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 


The last packet successfully received from the server was 4,665,488 milliseconds ago.



检查MySQL上wait_timeout的值。

你可以使用DBCP设置,例如,validationQuery,testOnBorrow和testWhileIdle 。

一个"belt "和"braces"的配置,可能会以牺牲性能为代价来解决您的问题:


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">


 <property name="driverClassName" value="${jdbc.driverClassName}"/>


 <property name="url" value="${jdbc.url}"/>


 <property name="username" value="${jdbc.username}"/>


 <property name="password" value="${jdbc.password}"/>


 <property name="validationQuery" value="SELECT 1"/>


 <property name="testOnBorrow" value="true"/>


</bean>



以上将在您每次从池中borrow时测试连接。

...