Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-02-2009, 11:42 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 406
Rep Power: 3
tim is on a distinguished road
Default Invalid Cursor State, what it is and how to avoid it
Greetings everyone.

What is an Invalid Cursor State?
The Invalid Cursor State exception thrown by a ResultSet from an MS Access database is an infamous one. Surprisingly, there aren't many forums that discuss this problem in depth. But there are many people asking the same questions. I've been searching Google on this topic and made the following conclusions. This exception is thrown if a result set is requested to access records with invalid positions in that set. For example, the record before the first, if the set is nonempty. Or the record after the last, if the set is nonempty. Or, if the set is empty, any record position.
What causes it, how can I fix it?
The most common source of this exception, is calling one of the data type getters from a result set without calling next(). The correct way to handle a result set is to traverse its records once. Always close the result set's statement when done. It is good practice to use a new statement for each result set. This avoids other exceptions caused by closed result sets or closed statements.
Code:
while (resultSet.next()) {
    Object[] record = new Object[columnCount];
    for (int i = 1; i <= columnCount; i++) {
        record[i - 1] = resultSet.getObject(i);
    }
    addRecord(record);
}
resultSet.getStatement().close();
Another cause is the easy navigation solution most forum members recommend. Scroll Insensitive result sets.
Code:
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
So why is this a problem? Well, it turns out that this is partially supported. For most queries, result sets are scrollable. But, occasionally the Invalid Cursor State exception is thrown when least expected. Complicated queries, or queries executing concurrently will cause this exception to be thrown. So how can we fix this? If the result set is relatively small, cache it. In other words, represent the result set in some data structure in memory by reading the result set linearly and then closing its statement. Now you can access the data in any direction or manner. The obvious problem occurs when the result set is large. In this case, it would be better not to cache it, but restrict access to a linear fashion. That is, one record accessed directly after the previous one.

Thanks for reading. ; )
__________________
Eyes dwelling into the past are blind to what lies in the future.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Time constraints on object state Flipke New To Java 4 04-11-2009 07:12 PM
Checking toggle state of a menu item xsive SWT / JFace 1 09-22-2008 03:42 PM
I have problem in parsing state machine diagram Saniya Eclipse 0 06-10-2008 08:37 AM
Persisting state of objects JavaForums Java Blogs 0 12-11-2007 06:20 PM
Concurrent Hierarchical State Machine 4.3 levent Java Announcements 0 08-03-2007 05:44 PM


All times are GMT +2. The time now is 05:27 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org