今天遇到了一个问题,一个项目,放到服务器(tomcat)下面的跑,但第二天,总是报错,项目还不能跑

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 1 milliseconds ago.  The last packet sent successfully to the server was 1 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1127)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3715)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3604)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4155)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1307)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2931)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2929)
at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:131)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:493)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:59)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:73)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:60)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
at org.activiti.engine.impl.db.DbSqlSession.selectListWithRawParameter(DbSqlSession.java:443)
at org.activiti.engine.impl.db.DbSqlSession.selectList(DbSqlSession.java:434)
.......

发现这个问题是MySQL的坑:

  wait_timeout的作用是,设置非交互连接(就是指那些连接池方式、非客户端方式连接的)的超时时间,默认是28800,就是8小时,超过这个时间,mysql服务器会主动切断那些已经连接的,但是状态是sleep的连接。而我们后端程序是运行在windows下的,所以安装的myodbc,并使用ado方式连接的,也就是连接池方式,这种方式的坏处是,当服务器端去连接mysql的时候,连接池里的连接已经被mysql主动断开,这时取回的连接就是null,再加上程序里对此没有做判断的话,接下来的对数据库的一系列的操作都会出现问题。

解决方案:

《1》增加 MySQL 的 wait_timeout 属性的值 (不推荐)

  修改mysql安装目录下的配置文件 my.ini文件,这两个参数的默认值是8小时(60*60*8=28800)

wait_timeout=1814400 
interactive_timeout=1814400 

《2》减少连接池内连接的生存周期

  减少连接池内连接的生存周期,使之小于上一项中所设置的wait_timeout 的值。修改 c3p0 的配置文件,在 Spring 的配置文件中设置:

<bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="maxIdleTime"value="1800"/>
<!--other properties -->
</bean>

《3》定期使用连接池内的连接

  定期使用连接池内的连接,使得它们不会因为闲置超时而被 MySQL 断开。修改 c3p0 的配置文件,在 Spring 的配置文件中设置:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="preferredTestQuery" value="SELECT 1"/>
<property name="idleConnectionTestPeriod" value="18000"/>
<property name="testConnectionOnCheckout" value="true"/>
</bean>

MySQL中wait_timeout的坑的更多相关文章

  1. 被mysql中的wait_timeout坑了

    今天被mysql里的wait_timeout坑了         网上能搜到很多关于mysql中的wait_timeout相关的文章,但是大多数只是说明了他的作用,而且都说这个参数要配合那个inter ...

  2. mysql中now()函数的使用,还有oracle的sysdate,可能埋下的坑

    mysql中now()函数的使用,还有oracle的sysdate 在需求中如果系统中药添加当前操作的时间那么很简单的一个操作在写sql的时候直接在这个字段对应的位置写上now()函数就可以了,这样就 ...

  3. MySQL中字符串与数字比较的坑

    公司项目代码中,某枚举字段数据库表中类型是char(1),在代码中,误以为是TINYINT,所以用数字筛选,后来发现结果不对.发现了一个现象,用数字0筛选会把所有的记录给筛选出来. 经过排查发现是在M ...

  4. MySQL中字段类型为timestamp的小坑

    之前遇到过一个MySQL的字段为timestamp类型的小坑. MySQL中一个字段存储时间类型数据的时候,该字段的类型如果为timestamp类型的话,最多只能存储到2038-01-19 11:14 ...

  5. MySQL中interactive_timeout和wait_timeout的区别【转】

    在用mysql客户端对数据库进行操作时,打开终端窗口,如果一段时间没有操作,再次操作时,常常会报如下错误: ERROR 2013 (HY000): Lost connection to MySQL s ...

  6. DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量

    DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...

  7. VS2017 + EF + MySQL 我使用过程中遇到的坑

    原文:VS2017 + EF + MySQL 我使用过程中遇到的坑 写在前面: 第一次使用MySQL连接VS的时候本着最新版的应该就是最好的,在MySQL官网下载了最新版的MySQL没有并且安装完成之 ...

  8. mysql优化过程中遇见的坑(mysql优化问题特别注意)

    不要听信你看到的关于优化的“绝对真理”,包括本文所讨论的内容,而应该是在实际的业务场景下通过测试来验证你关于执行计划以及响应时间的假设. 单条查询最后添加 LIMIT 1,停止全表扫描. 对于char ...

  9. MySql数据库GROUP BY使用过程中的那些坑

    MySql数据库GROUP BY使用过程中的那些坑 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组. 特别注意: group by 有一个原则,就是 select 后面的所有 ...

随机推荐

  1. python基础 - 控制语句

    判断-if mood = True if mood: print('mood ok'); else: print('mood not OK') if-elif-else if a == 1: pass ...

  2. ApplicationContext中Bean的生命周期

    引言 Bean应用上下文中的生命周期和在BeanFactory中生命周期类似,不同的是,如果Bean实现了org.springframework.context.ApplicationContextA ...

  3. 【微软编程一小时】题目1 : Arithmetic Expression

    时间限制:2000ms 单点时限:200ms 内存限制:256MB 描写叙述 Given N arithmetic expressions, can you tell whose result is ...

  4. Swift中的反射

    原文:http://www.cocoachina.com/applenews/devnews/2014/0623/8923.html Swift 事实上是支持反射的.只是功能略弱. 本文介绍主要的反射 ...

  5. map && multimap

    map map 的意思是映射.用法一般是     map<char, int>mp 按照我的理解,map 类似于一个高级的数组.前面的数据类型 char 相当于下脚标,而数组元素的值就对应 ...

  6. 30个你 “ 不可能全部会做 ” 的javascript题目

    1,以下表达式的运行结果是: ["1","2","3"].map(parseInt) A.["1","2&qu ...

  7. 第一行代码 3-2-2 软件也要拼脸蛋-UI界面-更强大的滚动条- 卡片

    <LinearLayout android:orientation="vertical" android:layout_width="match_parent&qu ...

  8. 编译安装nginx,并使用systemd管理nginx

    #tar zxvf nginx-1.8.1.tar.gz #cd nginx-1.8.1/ #make && make install #cat /etc/systemd/system ...

  9. Linux中运行SpringBoot项目,永久运行

    将写好的springboot项目打成jar包: 项目右键 -- Run As -- Maven build... ---此时出现下图 1.Goals 中填写:install 2.Skip Tests复 ...

  10. POJ1251(Kruskal水题)

    https://vjudge.net/problem/POJ-1251 The Head Elder of the tropical island of Lagrishan has a problem ...