播放2.6数据库:始终关闭JDBC连接

时间:2017-09-08 10:23:03

标签: java scala jdbc playframework-2.0

我在Playgres上使用Play 2.6.3并尝试异步从Play.db.Database获取连接。这是代码:

public static CompletionStage<Connection> getConnection(Database database, boolean readOnly,
    DatabaseExecutionContext executionContext) {
return supplyAsync(() -> {
    return database.withTransaction(connection -> {
        try {
            return connection;  // checked here and found it's a non-closed connection
        } catch (Exception e) {
            throw new SQLException("Could not get JDBC Connection", e);
        }
    });
}, executionContext);

}

但是,每当我尝试使用dao层的连接时,我总是得到已经关闭的连接,尽管在上面的方法中我得到一个非关闭的连接。这是代码:

DBUtil.getConnection(db, true, executionContext).thenApplyAsync(c -> {
        try {
            System.out.println("closed : " + c.isClosed());
        } catch (Exception e) {
            e.printStackTrace();
        }
        DBUtil.releaseConnection(c);
        return "";
    });

我在这里做错了吗?

0 个答案:

没有答案
相关问题