如果使用statement.setQueryTimeout,连接不会关闭

时间:2019-12-06 08:17:20

标签: java mysql spring jdbc datasource

我从春季开始使用SingleConnectionDataSource编写了简单的测试:

public void checkDataSourceHealth() throws SQLException {
    SingleConnectionDataSource ds = new SingleConnectionDataSource();
    ds.setUrl("jdbc:mysql://localhost:3306/default");
    ds.setUsername("sys");
    ds.setPassword("qwerty");
    ds.setDriverClassName("com.mysql.cj.jdbc.Driver")

    try (Connection connection = dataSource.getConnection()) {
        try (Statement statement = connection.createStatement()) {
            statement.setQueryTimeout(2);
            statement.execute("SELECT SLEEP(3)"); //just for test. Will be 'SELECT 1'
        }
    } catch (MySQLTimeoutException ex) {
        //data base is BUSY
    } catch (Exception e) {
        log.warn("Check dataSource health failed");
        throw e;
    } finally {
        dataSource.destroy();
    }
}

我只想检查我的请求是否已完成2秒钟以上,请将状态设置为“忙”。它可以正常工作,但是在此之后,我检查了show processlist;,看到了实时连接: enter image description here

但是为什么呢?执行此查询后,我关闭了所有程序。为什么连接还活着?

0 个答案:

没有答案
相关问题