MySQL隔天连接出错的问题

本文探讨了使用Struts+Hibernate+MySQL构建的系统中隔天登录时出现的连接异常问题,并提供了详细的解决方案,包括调整MySQL配置参数和Hibernate连接池配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 MySQL隔天连接出错的问题
用Struts+Hibernate+MySQL构建的B/S系统24小时运行,在隔一天再进行登录的时候就会出现下面的错误: com.mysql.jdbc.CommunicationsException MESSAGE: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.net.SocketException MESSAGE: Software caused connection abort: socket write error STACKTRACE: java.net.SocketException: Software caused connection abort: socket write error         at java.net.SocketOutputStream.socketWrite0(Native Method)         at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)         at java.net.SocketOutputStream.write(SocketOutputStream.java:136)         at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65 )         at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)         at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2690)         at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2619)         at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1552)         at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)         at com.mysql.jdbc.Connection.execSQL(Connection.java:2978)         at com.mysql.jdbc.Connection.execSQL(Connection.java:2902)         at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.ja va:933)         at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java: 1027)         at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java: 137)         at org.hibernate.loader.Loader.getResultSet(Loader.java:1676)         at org.hibernate.loader.Loader.doQuery(Loader.java:662)         at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Lo ader.java:223)         at org.hibernate.loader.Loader.doList(Loader.java:2147)         at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)         at org.hibernate.loader.Loader.list(Loader.java:2021)         at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)         at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.ja va:296)         at org.hibernate.impl.SessionImpl.list(SessionImpl.java:992)         at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)         at com.mobile_front.dao.TblUserinfoDAO.findByProperty(TblUserinfoDAO.jav a:100) ...................... ...................... 因为在晚上的时候没有用户进行连接,Socket早就已经超时了。除非重新启动Web Server,否则连接始终失败。 专家建议:    注意:我不赞成 Autoreconnect 功能,在以后的发行版本中,它最终会被移除。 在这种特殊情况下, 它不起作用的原因是:在 3.0.11 以后, autoreconnect 的方法变得更加安全了并且和阿 utoCommit 状态有关联,这样的话就能使当前‘ in-flight ’事务失败(如果你在失败后,再次试图连接事务,就会重新被连接)。请查看相关解释文档,文档的修复故障中包括如何正确实用这个属性。 无论在哪种情况下,   如果 TCP/IP 在没有连接的情况下并且还不会冒着数据库被瘫痪的危险,是没有百分之百的方式使得 JDBC 驱动器被自动重连接的,这也是为何要移除 Autoreconnect 功能的原因。 不管网络发生什么情况,    JDBC 都不会说明这个连接是否处于连接状态。 JDBC driver 客户端负责处理网络故障,只有应用程序本身(实际上是应用程序的开发者)知道如何正确应对事务失败的情况。在服务器上过期的‘ Wait timeout ’基本上是服务器给与的强制性网络故障。 你通过把‘ Wait timeout ’设置的高点儿就可以改正它,尽管如此,作为一个开发者,你的代码应该包含相关的异常处理并采取适当的恢复措施,不要都把它们传到调用堆中。    连接错误总是有一个 SQL 状态‘ 08 ’。如果你发现它的话,你可以再连接一次并重试事务(如果是适当的话)    不管什么原因,如果这样不起作用的话,配置你的连接池来测试是否处于连接状态并且那些长时间闲置连接(所有的连接池都能这样做,但是它们的配置取决于池子)。 作者说明:在MYSQL4.1以后的版本当中,没有“wait_timeout”变量,由 interactiveClient代替 参考资料:http://dev.mysql.com/doc/connector/j/en/cj-configuration-properties.html      [ Kirk Wylie ]:翻译如下:     如果有一个事务在运行的话,就能得到一个特别简单的例子:      --- 如果连接在 autoCommit 模式下 ,autoReconnect 是安全的。      --- 如果连接不在 autoCommit 模式下,但是没有打开的事务, autoReconnect 是安全的。(因为连接可能在池在里面)     --- 如果连接不在 autoCommit 模式下,有打开的事务,这样就会抛出异常。     这样会解决你不在 autoRecom/nnect 模式下的忧虑并且会保留应用程序的功能性。    尽管如此,在它改变的时候,在改变日志中没有任何记录说明发生了改变,这点令我很烦。由于这种改变很可能破坏我的应用程序,这种变化因该添加到 CHANGE 文件中。    如下是我个人的做法:   (STRUTS+SPRING+HIBERNATE) 1、mysql配置中的wait_timeout值配大点,默认是8小时,最好是比连接池中的idel_timeout值大。否则mysql会在wait_timeout的时间后关闭连接,然而连接池还认为该连接可用,这样就会产生错误 。 首先:启动mysql的命令行控制台,然后输入show variables;在最下面可以看到wait_timeout变量以及属性值,应该为28800;然后通过mysql命令,set wait_timeout = 864000;将最大超时时间设置为10天。 2、在HIBERNATE当中加入:    
.. < property name = " hibernate.connection.url " > jdbc:mysql: // 192.168.100.111/eai?autoReconnect=true</property> < property name = " enableDeprecatedAutoreconnect " > true </ property > <!--  如下的语句是使用第三方的c3p0来建立连接池 --> <property name="c3p0.min_size">10</property> <property name="c3p0.max_size">50</property> <!--  看到了没有,这个timeout一定要大于MYSQL默认的28800(秒) --> <property name="c3p0.timeout">2000000000000</property> <property name="c3p0.max_statements">50</property> ..
     这是网上找的
     具体实践代码如下
    proxool.xml
    <?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
 <proxool>   <alias>DBPool</alias>   <driver-url>    jdbc:mysql://localhost:3306/shujuku
  </driver-url>
  <driver-class>com.mysql.jdbc.Driver</driver-class>   <driver-properties>    <property name="user" value="root" />    <property name="password" value="" />   </driver-properties>   <house-keeping-sleep-time>90000</house-keeping-sleep-time>   <maximum-new-connections>20</maximum-new-connections>   <prototype-count>5</prototype-count>   <maximum-connection-count>100</maximum-connection-count>   <minimum-connection-count>10</minimum-connection-count>  </proxool>
</something-else-entirely>
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">
<!-- Generated by MyEclipse Hibernate Tools.                   --> <hibernate-configuration>  <session-factory>   <property name="hibernate.show_sql">true</property>   <property name="hibernate.connection.provider_class">    org.hibernate.connection.ProxoolConnectionProvider   </property>   <property name="hibernate.proxool.pool_alias">DBPool</property>   <property name="hibernate.proxool.xml">proxool.xml</property>   <property name="connection.autocommit">true</property>   <property name="connection.password"></property>   <property name="connection.release_mode">auto</property>   <mapping resource="com/shujuku/bean/Member.hbm.xml" />   <mapping resource="com/shujuku//bean/Memberdetail.hbm.xml" />   <mapping resource="com/shujuku/bean/Memberfee.hbm.xml" />   <mapping resource="com/shujuku//bean/Agent.hbm.xml" />   <mapping resource="com/shujuku/bean/Cashcard.hbm.xml" />   <mapping resource="com/shujuku//bean/Buyrecord.hbm.xml" />   <mapping resource="com/shujuku/bean/Cashrecord.hbm.xml" />  </session-factory> </hibernate-configuration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值