oracle querytimeout,聊聊pg jdbc的queryTimeout及next方法

本文深入探讨了Pg JDBC中的Statement查询超时(queryTimeout)设置及其影响,指出queryTimeout通过添加定时器控制,并在超时时发送取消查询指令到数据库。同时,文章提到了resultSet.next()方法在获取数据时的行为,它不直接受queryTimeout限制,但受socket超时控制。总结中强调了超时管理的重要性,以避免内存溢出和过多的定时器任务。
摘要由CSDN通过智能技术生成

本文主要介绍一下pg jdbc statement的queryTimeout及resultSet的next方法

实例程序

@Test

public void testReadTimeout() throws SQLException {

Connection connection = dataSource.getConnection();

//https://jdbc.postgresql.org/documentation/head/query.html

connection.setAutoCommit(false); //NOTE 为了设置fetchSize,必须设置为false

String sql = "select * from demo_table";

PreparedStatement pstmt;

try {

pstmt = (PreparedStatement)connection.prepareStatement(sql);

pstmt.setQueryTimeout(1); //NOTE 设置Statement执行完成的超时时间,前提是socket的timeout比这个大

pstmt.setFetchSize(5000); //NOTE 这样设置为了模拟query timeout的异常

System.out.println("ps.getQueryTimeout():" + pstmt.getQueryTimeout());

System.out.println("ps.getFetchSize():" + pstmt.getFetchSize());

System.out.println("ps.getFetchDirection():" + pstmt.getFetchDirection());

System.out.println("ps.getMaxFieldSize():" + pstmt.getMaxFieldSize());

ResultSet rs = pstmt.executeQuery(); //NOTE 设置Statement执行完成的超时时间,前提是socket的timeout比这个大

//NOTE 这里返回了就代表statement执行完成,默认返回fetchSize的数据

int col = rs.getMetaData().getColumnCount();

System.out.println("============================");

while (rs.next()) { //NOTE 这个的timeout由socket的超时时间设置,oracle.jdbc.ReadTimeout=60000

for (int i = 1; i <= col; i++) {

System.out.print(rs.getObject(i));

}

System.out.println("");

}

System.out.println("============================");

} catch (SQLException e) {

e.printStackTrace();

} finally {

//close resources

}

}

PgStatement

ostgresql-9.4.1212.jre7-sources.jar!/org/postgresql/jdbc/PgStatement.java

executeInternal()

private void executeInternal(CachedQuery cachedQuery, ParameterList queryParameters, int flags)

throws SQLException {

closeForNextExecution();

// Enable cursor-based resultset if possible.

if (fetchSize > 0 && !wantsScrollableResultSet() && !connection.getAutoCommit()

&& !wantsHoldableResultSet()) {

flags |= QueryExecutor.QUERY_FORWARD_CURSOR;

}

if (wantsGeneratedKeysOnce || wantsGeneratedKeysAlways) {

flags |= QueryExecutor.QUERY_BOTH_ROWS_AND_STATUS;

// If the no results flag is set (from executeUpdate)

// clear it so we get the generated keys results.

//

if ((flags & QueryExecutor.QUERY_NO_RESULTS) != 0) {

flags &= ~(QueryExecutor.QUERY_NO_RESULTS);

}

}

if (isOneShotQuery(cachedQuery)) {

flags |= QueryExecu

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值