How to set Cursor type in JDBC?

I'm running tomcat and have some jsp pages that display a subset of a table. I show 20 rows at a time on a single page. When the table has large amounts of data, the jsp page doesn't render. I'm guessing that the ResultSet is using a client side cursor. I've worked with ASP in the past, and we always used server side forward only cursors, and never had any problems with large amounts of data. Our database is oracle 10g.

How can I specify a server-side forward-only cursor in JDBC?

Answers


The oracle driver implements server-side cursors via the FetchSize property.

Unfortunately, JDBC doesn't explicitly allow for setting client vs server-side cursors, so different drivers implement it in different ways. Here are the other links that helped:

Fetch Size Cursors Oracle Driver


Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY);
ResultSet rs = stmt.executeQuery(sql);

This should set it but apparently some drivers ignore it.

You could always try and set it again at ResultSet level.

rs.setFetchDirection(ResultSet.TYPE_FORWARD_ONLY);

Hope that helps.


Not quite answering the question, but have you considered explicitly adding paging to your SELECT query using ROWNUM or ROWNUMBER in your WHERE clause?

eg: for the second page of data, 20 element page size:

SELECT * 
  FROM MyDataObjects
  WHERE rownum > 20 AND rownum < 41

This would ensure that at most one page of records are returned, removing the large cursor issue.


Need Your Help

Get highest user role in Symfony2

symfony fosuserbundle roles

I'm using Symfony2 with FOSUserBundle. I need to get the highest role for a User.