tomcat服务器隔一段时间不访问,就会与MySQL数据库断开连接
tomcat服务器隔一段时间不访问,就会与MySQL数据库断开连接,并报错:
java.net.SocketException: Software caused connection abort: socket write error
原因是MySQL的连接超时时间是8小时。若空闲超过8小时,MySQL就会自动断开连接。
方法一:
使用连接池可以解决这个问题,这里使用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 》
方法二:
一般的解决方法大多是在数据库连接字符串中增加“autoReconnect=true ”选项。但是这只对mysql4以前的版本有效。在最新的mysql中是无效的。其实要解决这个问题也有一个简单的方法,就是修改mysql的启动参数。缺省情况下mysql的timeout时间是28800秒,正好是8小时,增加一个0就可以了。
windows下在my.ini文中增加:
interactive_timeout=
还没有评论,来说两句吧...