SpringBootWEB项目数据库连接关闭异常

1.问题描述
项目启动后,有30分钟一次的定时任务,在过一段时间后,就会抛出连接已关闭的异常,重启后又恢复正常
2.相关代码
多数据源配置:

图片说明

public class UfOau8userServiceImpl{
    private final static Logger logger = LoggerFactory.getLogger(UfOau8userServiceImpl.class);
    @Autowired
    @Qualifier("OAJdbcTemplate")
    private JdbcTemplate oaJdbcTemplate;

    public Integer selectOAU8User(String cPersonCode){
        String sql="select count(id) as idnum from uf_OAU8User where cPersonCode='"+cPersonCode+"'";
        logger.info("查询OA数据库===="+sql);
        try{
            Integer idnum =oaJdbcTemplate.queryForObject(sql,Integer.class);
            return idnum;
        }catch (Exception e){
            logger.error(this.getClass().getName()+".selectOAU8User根据U8人员编号获取OA人员对应表数据异常="+e);
            logger.error(this.getClass().getName()+".selectOAU8User异常SQL="+sql);
            return -1;
        }
    }
}

3.报错信息
2018-12-06 15:00:00.178 [pool-1-thread-1] ERROR com.yt.service.oa.UfOau8userServiceImpl - com.yt.service.oa.UfOau8userServiceImpl.selectOAU8User根据U8人员编号获取OA人员对应表数据异常=org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException for SQL [select count(id) as idnum from uf_OAU8User where cPersonCode='01027']; SQL state [null]; error code [0]; The connection is closed.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.

没看到你的数据源配置bean,估计是事务控制有问题

应该有连接池吧,这个一般是链接闲置太久了,数据库把链接断开了,所以需要你检查数据库链接池的配置和数据库的配置,超时配置和闲置时间配置之类的

添加自动重连
https://blog.csdn.net/dodott/article/details/54616333

一般跟数据库源和客户端配置无关,经验之谈看下网络环境是否经过防火墙等类似设备,请检查这些设备是否开启longtime等长连接功能。