这个一般会出现在连接池中,导致连接池数量不够然后连接失败,所以要kill
我们kill的都是sleep的,就是没有释放但是不用的链接
前言
sql中的show full processlist是查看线程,实际就是查这个表
select * from information_schema.processlist where COMMAND = 'Sleep' and Time>30
然后在配上kill id,就能释放,为了以后方便寻找,我把他写成了接口,我通过shell脚本的curl 调用这个接口,就能实现轮询释放。
代码实现
controller层代码
@GetMapping("kill")public RsJsonBean killProcess(@Param("key") String key) {SqlSession mybaiteSession = null;try {if (key.equals("#sdfada!")) {//密钥,当这个成立时,则会释放所有线程,腾出空间mybaiteSession =DBCPUtil.getMybaiteSession();infoMapper = mybaiteSession.getMapper(InfoMapper.class);List<HashMap> hashMaps = infoMapper.selectProcess();for (int i = 0; i < hashMaps.size(); i++) {HashMap hashMap = hashMaps.get(i);int id =Integer.parseInt(hashMap.get("ID").toString());infoMapper.killProcess(id);}mybaiteSession.commit();return new RsJsonBean(1, "成功释放");}//密钥不匹配return new RsJsonBean(0, "释放失败:" + "密钥不匹配");} catch (Exception e) {return new RsJsonBean(0, "释放失败:" + e.getMessage());} finally {if (mybaiteSession != null) {mybaiteSession.close();}}}
mybaites代码,sql
//查询所有符合条件的线程 List<HashMap> selectProcess(); //释放某个线程 int killProcess(@Param("id") int id);
<select id="selectProcess" resultType="java.util.HashMap">select * from information_schema.processlist where COMMAND = 'Sleep' and Time>15
</select><delete id="killProcess">kill #{id}
</delete>
shell脚本
通过curl方式的特殊字符不能用。
curl -XGET http://127.0.0.1:8087/info/kill?"key=23aoTest"
-XGET代表get请求,crul中参数拿""标起来
#!/bin/bash
while true
do
echo "开始,请等待5秒"sleep 5secho `curl -XGET http://127.0.0.1:8087/info/kill?"key=23aoTest"`echo `date`echo "-----"
done