Does a ResultSet load all data into memory or only when requested?

前端 未结 5 805
眼角桃花
眼角桃花 2020-11-30 04:57

I have a .jsp page where I have a GUI table that displays records from an Oracle database. This table allows typical pagination behaviour, such as \"FIRST\", \"NEXT\", \"PRE

相关标签:
5条回答
  • 2020-11-30 05:05

    The Java ResultSet is a pointer (or cursor) to the results in the database. The ResultSet loads records in blocks from the database. So to answer your question, the data is only fetched when you request it but in blocks.

    If you need to control how many rows are fetched at once by the driver, you can use the setFetchSize(int rows) method on the ResultSet. This will allow you to control how big the blocks it retrieves at once.

    0 讨论(0)
  • 2020-11-30 05:06

    lets say we have a table that contains 500 records in it

    PreparedStatement stm=con.prepareStatement("select * from table");
    stm.setFetchSize(100);// now each 100 records are loaded together from the database into the memory,
    // and since we have 500 5 server round trips will occur.
    ResultSet rs = stm.executeQuery();
    rs.setFetchSize (50);//overrides the fetch size provided in the statements,
    //and the next trip to the database will fetch the records based on the new fetch size
    
    0 讨论(0)
  • 2020-11-30 05:10

    The best idea is make a sub query and display 100 or 1000 rows at a time/in single page. And managing the connection by connection pooling.

    To make a sub query you can use Row count in oracle and Limit in MY SQL.

    0 讨论(0)
  • 2020-11-30 05:15

    The JDBC spec does not specify whether the data is streamed or if it is loaded into memory. Oracle streams by default. MySQL does not. To get MySQL to stream the resultset, you need to set the following on the Statement:

        pstmt = conn.prepareStatement(
            sql,
            ResultSet.TYPE_FORWARD_ONLY,
            ResultSet.CONCUR_READ_ONLY);
        pstmt.setFetchSize(Integer.MIN_VALUE);
    
    0 讨论(0)
  • 2020-11-30 05:22

    While the JDBC spec does not specify whether or not the all data in the result set would get fetched, any well-written driver won't do that.

    That said, a scrollable result set might be more what you have in mind: (link redacted, it pointed to a spyware page)

    You may also consider a disconnected row set, that's stored in the session (depending on how scalable your site needs to be): http://java.sun.com/j2se/1.4.2/docs/api/javax/sql/RowSet.html

    0 讨论(0)
提交回复
热议问题