我在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 "";
});
我在这里做错了吗?