【发布时间】:2019-04-12 14:18:02
【问题描述】:
我有一个到 PostgreSQL 数据库的连接(使用 PostgreSQL JDBC 驱动程序),并且我在其上设置了网络超时(使用 setNetworkTimeout 方法),这有点奇怪。
当我将连接用于select * from table 之类的简单查询时,这需要很多时间,但它工作正常(等待查询返回结果)。 但是当我将连接用于使用函数的查询(如select max(a) from table)时,这也需要很多时间,它会由于超时而引发异常。
示例代码:
// Queries which takes more than 5 seconds
String bigQuery = "select * from data.bigtable tb1 inner join data.bigtable tb2 on tb1.a like '%a%'";
String bigQueryWithFunction = "select max(tb1.a) from data.bigtable tb1 inner join data.bigtable tb2 on tb1.a like '%a%'";
// Creating a connection with 5 seconds network timeout
Connection con = source.getConnection();
con.setNetworkTimeout(Executors.newSingleThreadExecutor(), 5000);
con.setAutoCommit(false);
Statement st2 = con.createStatement();
st2.execute(bigQueryWithFunction); // This line DOES throws an exception
st2.execute(bigQuery); // This line DOES NOT throws an exception
(忽略查询的逻辑。)
谁能解释一下为什么会这样?
【问题讨论】:
标签: java postgresql jdbc timeout pg-jdbc