java.net.SocketException: Software caused connection abort: socket write error

来源:互联网 发布:四柱汉诺塔 python 编辑:程序博客网 时间:2024/05/18 00:43

项目的后台数据库是Mysql,最近出现了java.net.SocketException: Software causedconnection abort: socket write error  这个异常。

      在网上google了一下,都说是因为Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲如果超过8个小时,Mysql将自动断开该connection。这样就出现了上面这个问题。

      网上的解决方法有几种:

      第一种:在mysql安装目录下找到my.ini文件中添加超时限制:在该文件最后添加一行:wait_timeout=2880000。这样把之前的超时限制把8小时(28800)扩大为20天(20*24*60*60=1728000)。这样重启了mysql后,再在其中输入命令:show global variables like "wait_timeout"; ,查看超时是否已修改为:1728000。实际操作中,我修改的是C:\WINDOWS目录下的my.ini,这是我的Mysql数据库所使用的描述文件,在该文件的[mysqld]一节中,添加了wait_timeout=1728000。重启mysql后,执行上面的查询,返回的查询结果如下:

mysql> show global variables like "wait_timeout";+---------------+-------------+| Variable_name | Value   |+---------------+-------------+| wait_timeout  | 1728000 |+---------------+-------------+1 row in set (0.00 sec)

 

      最后,重启tomcat,OK,服务器能正常访问。

     

      第二种,通过c3p0的idleConnectionTestPeriod来控制:可以将它的值设定的比Mysql的默认wait_timeout小就行了。

      <property name="idleConnectionTestPeriod" value="18000"/>

      使用连接池可以解决这个问题,这里使用c3p0:
      修改hibernate.cfg.xml文件

复制代码
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property><property name="hibernate.c3p0.min_size">8</property><property name="hibernate.c3p0.max_size">200</property><property name="hibernate.c3p0.timeout">600</property><property name="hibernate.c3p0.max_statements">0</property><property name="hibernate.c3p0.idle_test_period">60</property><property name="hibernate.c3p0.acquire_increment">2</property><property name="hibernate.c3p0.validate">true</property>
阅读全文
0 0
原创粉丝点击