我有一个到 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
(忽略查询的逻辑。)
有人可以向我解释为什么会这样吗?