mysql连接空闲8小时自动断开问题DBCP解决方案

来源:互联网 发布:彩票走势软件 编辑:程序博客网 时间:2024/04/29 19:06
  Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该 connection。connections如果空闲超过8小时,Mysql将其断开,而DBCP并不知道该connection已经失效,如果这时有 Client请求connection,DBCP将该失效的Connection提供给Client,将会造成上面的异常。

    mysql配置中my.cnf 的wait_timeout值一定要大于等于连接池种的idle_timeout 值。否则mysql会在wait_timeout的时间后关闭连接,然而连接池还认为该连接可用,这样就会产生异常。

    org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: : Communications link failure Last packet sent to the server was 1 ms ago. org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    DBCP连接池说明:driverClassName url username password 上面四个分别是驱动,连接字符串,用户名和密码

    maxActive 连接池支持的最大连接数

    maxIdle 连接池中最多可空闲maxIdle个连接

    minIdle 连接池中最少空闲maxIdle个连接

    initialSize 初始化连接数目

    maxWait 连接池中连接用完时,新的请求等待时间,毫秒

    timeBetweenEvictionRunsMillis和minEvictableIdleTimeMillis一起使用,每timeBetweenEvictionRunsMillis毫秒秒检查一次连接池中空闲的连接,把空闲时间超过minEvictableIdleTimeMillis毫秒的连接断开,直到连接池中的连接数到minIdle为止 主要把这两个参数加上就好。

    minEvictableIdleTimeMillis 连接池中连接可空闲的时间,

    毫秒removeAbandoned true,false,是否清理

    removeAbandonedTimeout秒没有使用的活动连接,清理后并没有放回连接池

    removeAbandonedTimeout 活动连接的最大空闲时间

    logAbandoned true,false,连接池收回空闲的活动连接时是否打印消息

    minEvictableIdleTimeMillis,removeAbandonedTimeout这两个参数针对的连接对象不样

    minEvictableIdleTimeMillis针对连接池中的连接 对象,

    removeAbandonedTimeout针对未被close的活动连接