Hibernate Session无法打开事务

时间:2014-06-05 07:02:25

标签: java mysql spring hibernate spring-mvc

有人可以告诉我以下异常吗。我尝试登录几次后,每天都会收到此异常。但是经过一些尝试后它又消失了,第二天再次出现。

以下是例外:

例外

> 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:637)
>   javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 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:335)
>   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)
>   $Proxy48.loginUser(Unknown Source)
>   com.sapat.competition_tracker.controller.AdminController.userLogin(AdminController.java:59)     sun.reflect.GeneratedMethodAccessor510.invoke(Unknown Source)
>   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   java.lang.reflect.Method.invoke(Method.java:616)
>   org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
>   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
>   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
>   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
>   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
>   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:637)
>   javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root
> cause
> 
> org.hibernate.TransactionException: JDBC begin failed: 
>   org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:96)
>   org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)
>   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:335)
>   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)
>   $Proxy48.loginUser(Unknown Source)
>   com.sapat.competition_tracker.controller.AdminController.userLogin(AdminController.java:59)     sun.reflect.GeneratedMethodAccessor510.invoke(Unknown Source)
>   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   java.lang.reflect.Method.invoke(Method.java:616)
>   org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
>   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
>   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
>   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
>   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
>   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:637)
>   javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root
> cause
> 
> com.mysql.jdbc.CommunicationsException: Communications link failure
> due to underlying exception: 
> 
> ** BEGIN NESTED EXCEPTION ** 
> 
> java.io.EOFException
> 
> STACKTRACE:
> 
> java.io.EOFException  at
> com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1963)   at
> com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2375)  at
> com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2874)    at
> com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)     at
> com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)  at
> com.mysql.jdbc.Connection.execSQL(Connection.java:3243)   at
> com.mysql.jdbc.Connection.setAutoCommit(Connection.java:5371)     at
> org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:331)
>   at
> org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setAutoCommit(PoolingDataSource.java:317)
>   at
> org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:91)
>   at
> org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)
>   at
> org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)
>   at
> org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
>   at
> org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
>   at
> org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
>   at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
>   at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
>   at $Proxy48.loginUser(Unknown Source)   at
> com.sapat.competition_tracker.controller.AdminController.userLogin(AdminController.java:59)   at sun.reflect.GeneratedMethodAccessor510.invoke(Unknown Source)    at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:616)     at
> org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
>   at
> org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
>   at
> org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
>   at
> org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
>   at
> org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
>   at
> org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
>   at
> org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
>   at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)     at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:717)  at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
>   at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>   at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
>   at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
>   at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
>   at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
>   at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
>   at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
>   at
> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
>   at
> org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
>   at
> org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
>   at java.lang.Thread.run(Thread.java:679)
> 
> 
> ** END NESTED EXCEPTION **
> 
> 
> 
> Last packet sent to the server was 1 ms ago.
>   com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2586)
>   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2874)
>   com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
>   com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
>   com.mysql.jdbc.Connection.execSQL(Connection.java:3243)
>   com.mysql.jdbc.Connection.setAutoCommit(Connection.java:5371)
>   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:1354)
>   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:335)
>   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)
>   $Proxy48.loginUser(Unknown Source)
>   com.sapat.competition_tracker.controller.AdminController.userLogin(AdminController.java:59)     sun.reflect.GeneratedMethodAccessor510.invoke(Unknown Source)
>   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   java.lang.reflect.Method.invoke(Method.java:616)
>   org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
>   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
>   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
>   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
>   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
>   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:637)
>   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

4 个答案:

答案 0 :(得分:2)

这是一个迟到的答案,但我遇到了同样的问题。这是因为c3po配置不当。

请使用以下c3po配置:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <property name="checkoutTimeout" value="30000" />
        <property name="idleConnectionTestPeriod" value="30"/>
        <property name="initialPoolSize" value="10" />
        <property name="maxIdleTime" value="30" />
        <property name="maxPoolSize" value="100" />
        <property name="minPoolSize" value="10" />
        <property name="maxStatements" value="10" />

</bean

答案 1 :(得分:1)

当池中的连接以某种方式过期而无法重新连接时会发生这种情况

https://forum.hibernate.org/viewtopic.php?f=1&t=985002

http://forums.mysql.com/read.php?39,139821,154688

尝试更改池设置(超时,idle_test_period,testConnectionOnCheckin等)

<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">100</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">60</prop>
<prop key="hibernate.c3p0.acquire_increment">2</prop>

或者如果你有像这样的DataSource尝试使用设置

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">

    <property name="minPoolSize" value="5"/>
    <property name="maxPoolSize" value="5"/>
    <property name="maxStatements" value="500"/>
    <property name="loginTimeout" value="300"/>
    <property name="initialPoolSize" value="5"></property>
    <property name="checkoutTimeout" value="1000"/>
    <property name="automaticTestTable" value="C3P0_TEST_TABLE"></property>
    <property name="testConnectionOnCheckin" value="true"></property>
    <property name="idleConnectionTestPeriod" value="60"></property>

</bean>

答案 2 :(得分:0)

此消息表明进程耗尽了套接字;也就是说 - 客户端和数据层服务器(MySQL所在的位置)之间的连接太多,太快了。

http://support.sas.com/kb/40/884.html

答案 3 :(得分:0)

这在servlet-context上为我修复了**。xml ** 添加maxidle,minidle和maxwait

        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>

            <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>

            **<beans:prop key="maxwait">10000</beans:prop>
            <beans:prop key="maxidle">25</beans:prop>
            <beans:prop key="minidle">5</beans:prop>**



        </beans:props>