1

In this code:

public static void viewTable(Connection con, String dbName)
    throws SQLException {

    Statement stmt = null;
    String query =
        "select COF_NAME, SUP_ID, PRICE, " +
        "SALES, TOTAL " +
        "from " + dbName + ".COFFEES";

    try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            System.out.println(coffeeName + "\t" + supplierID +
                               "\t" + price + "\t" + sales +
                               "\t" + total);
        }
    } catch (SQLException e ) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) { stmt.close(); }
    }
}

In particular, this code:

ResultSet rs = stmt.executeQuery(query);

Does this statement retrieve all rows to ResultSet, or partly? I need to load a table with 5 million rows in Hibernate and there is an OutofMemory error. It fails on this line:

List<Term> terms = em.createQuery(criteria).getResultList();

I need to find a way in Mysql & Hibernate to load this table with a memory issue. Thanks.

Drew
  • 24,851
  • 10
  • 43
  • 78
user697911
  • 10,043
  • 25
  • 95
  • 169
  • Do you really need all those records in memory? – juergen d Jun 02 '16 at 19:09
  • @juergend: It looks from the question as though the answer to that is very much "no", but Hibernate is loading the full resultset into memory on `Statement.executeQuery()`. – eggyal Jun 02 '16 at 19:14
  • I may not need to load into memory with the full table, but just curious to know whether there is a better way to do that. – user697911 Jun 02 '16 at 21:15
  • What I have found is that people that bring in all the data into arrays need to take tutorials on db use. Not the least of which reason is data staleness. – Drew Jun 03 '16 at 03:34
  • 2
    Possible duplicate of [JPA: what is the proper pattern for iterating over large result sets?](http://stackoverflow.com/questions/5067619/jpa-what-is-the-proper-pattern-for-iterating-over-large-result-sets) – Thierry Jun 03 '16 at 07:20
  • Generally in Production enviornment Pool size is greater and virtual memory allocated is also more than normal in Dev environment. So better limit the size for optimization. – prem30488 Jun 04 '16 at 08:47
  • Also you can use `criteria.setMaxSize(50);` – prem30488 Jun 04 '16 at 08:48

0 Answers0