java.sql。com.mysql.jdbc SQLException异常:连接。JDBC4Connection@1ab9c42关闭

时间:2021-07-12 22:51:45

Hi all I'm using JSP+Struts2+Tomcat6+Hibernate+mysql as my J2EE framework. the project works fine on local but when I put it on the server after some hours I get this error:

我使用JSP+Struts2+Tomcat6+Hibernate+mysql作为J2EE框架。这个项目在本地运行良好,但是当我在几个小时后将它放在服务器上时,我得到了这个错误:

'org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2297)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
    at org.hibernate.loader.Loader.list(Loader.java:2167)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at com.hposg.domain.HPOSG.getGameList(HPOSG.java:222)
    at com.hposg.controller.struts.GamesLoadActionAjax.execute(GamesLoadActionAjax.java:23)
    at sun.reflect.GeneratedMethodAccessor330.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    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:857)
    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:636)
Caused by: java.sql.SQLException: Connection com.mysql.jdbc.JDBC4Connection@1ab9c42 is closed.
    at org.apache.tomcat.dbcp.dbcp.DelegatingConnection.checkOpen(DelegatingConnection.java:354)
    at org.apache.tomcat.dbcp.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:246)
    at org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:302)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
    at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1596)
    at org.hibernate.loader.Loader.doQuery(Loader.java:717)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2294)
    ... 72 more

seems mysql closes some of it's connections and hibernate isn't aware of that so it leads the app to this condition. here's my hibernate.cfg.xml:

mysql似乎关闭了一些连接,而hibernate没有意识到这一点,因此它将应用程序引向这种情况。这是我的hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>

    <property name="connection.datasource">java:comp/env/jdbc/hposg</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="hibernate.max_fetch_depth">3</property>

    <!-- Mapping files -->
  </session-factory>
</hibernate-configuration>

as you see in the hibernate config, I get my connections through JNDI. the corresponding configs lies in a context.xml file:

正如您在hibernate配置中看到的,我通过JNDI获得连接。相应的构形位于上下文中。xml文件:

<Context>
    <Logger className="org.apache.catalina.logger.FileLogger"
             prefix="irvex_context_log" suffix=".txt"
             timestamp="true"/>

    <Resource name="jdbc/hposg" 
            global="jdbc/hposg"
            auth="Container"
            type="javax.sql.DataSource" 
            username="root"
            password="******"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://127.0.0.1:3306/hposg?autoReconnect=true&amp;characterEncoding=UTF-8&amp;tcpKeepAlive=true"
            maxActive="-1" 
            maxIdle="30"
            maxWait="10000"
            validationQuery="SELECT 1"         
            removeAbandoned="true"
            logAbandoned="true"
            />
</Context>

I suppose the validationQuery attribute would solve this issue but it doesn't. Any ideas?

我认为validationQuery属性可以解决这个问题,但它不能。什么好主意吗?

3 个解决方案

#1


2  

Try to add those properties in your datasource:

尝试在数据源中添加这些属性:

            testOnBorrow="true"
            testWhileIdle="true"

#2


1  

It is due to MySQL drops out the connection after some idle timeout, you can avoid this by

这是由于MySQL在一些空闲超时后退出连接,您可以通过以下方式避免这种情况

autoReconnect=true

in jdbc url

在jdbc url


Refer Also

也请参考

#3


1  

Or, you can try to increase the wait_timeout variable value in my.ini / my.cnf, the default value is 28800 seconds (8 hours).

或者,您可以尝试在my中增加wait_timeout变量值。ini / my.cnf默认值为28800秒(8小时)。

And maybe you want to refer the mysql document here: MySQL :: MySQL 5.5 Reference Manual :: 5.1.4 Server System Variables :: wait_timeout

您可能想在这里引用mysql文档:mysql:: mysql 5.5参考手册::5.1.4服务器系统变量::wait_timeout

The number of seconds the server waits for activity on a noninteractive connection before closing it. This timeout applies only to TCP/IP and Unix socket file connections, not to connections made using named pipes, or shared memory.

服务器在非交互式连接上等待活动的秒数,然后关闭它。这个超时仅适用于TCP/IP和Unix套接字文件连接,而不适用于使用命名管道或共享内存进行的连接。

#1


2  

Try to add those properties in your datasource:

尝试在数据源中添加这些属性:

            testOnBorrow="true"
            testWhileIdle="true"

#2


1  

It is due to MySQL drops out the connection after some idle timeout, you can avoid this by

这是由于MySQL在一些空闲超时后退出连接,您可以通过以下方式避免这种情况

autoReconnect=true

in jdbc url

在jdbc url


Refer Also

也请参考

#3


1  

Or, you can try to increase the wait_timeout variable value in my.ini / my.cnf, the default value is 28800 seconds (8 hours).

或者,您可以尝试在my中增加wait_timeout变量值。ini / my.cnf默认值为28800秒(8小时)。

And maybe you want to refer the mysql document here: MySQL :: MySQL 5.5 Reference Manual :: 5.1.4 Server System Variables :: wait_timeout

您可能想在这里引用mysql文档:mysql:: mysql 5.5参考手册::5.1.4服务器系统变量::wait_timeout

The number of seconds the server waits for activity on a noninteractive connection before closing it. This timeout applies only to TCP/IP and Unix socket file connections, not to connections made using named pipes, or shared memory.

服务器在非交互式连接上等待活动的秒数,然后关闭它。这个超时仅适用于TCP/IP和Unix套接字文件连接,而不适用于使用命名管道或共享内存进行的连接。