Showing posts with label java.sql.SQLException. Show all posts
Showing posts with label java.sql.SQLException. Show all posts

Java Exhausted Result Set / Oracle & Websphere 5.1|java.sql.SQLException: Exhausted Resultset

I am experiencing some problems with a simple query on a thin oracle driver. I am using a connection from the application server connection pool on a cluster machine. Basically this is my code:

try {
conn = DAOFactory.getConnection();
stmt = conn.prepareStatement(SELECT_STATEMENT);
stmt.setInt(1, var);
stmt.setFetchSize(max);
rs = stmt.executeQuery();

while( rs.next() ) {
long id = rs.getLong(1);
}

All this I incorporate into a try/catch block like this:

} catch(SQLException e) {
// some exception handling
} finally {
try {
if(stmt!=null)
stmt.close();
if(rs!=null)
rs.close();
if(conn!=null)
conn.close();
} catch (SQLException e1) {
// some exception handling
}
}

After some runs I get sporadically the “Exhausted Resultset” Exception at line rs.getLong(1). This indicates, that my result set is closed. This will imply that rs.next() returns true even if there are no results! This kind of strange behaviour I have already noticed by some other users.

My question: How can I ensure that rs.next() will return false if the result set is empty? Does anybody else encounter this problem before and have a smooth solution?

How to get original exception object from inside a nested or wrapped Exception (for example an EJBException or RemoteException)

  • When you have an javax.ejb.EJBException, you can use the getCausedByException() that returns a java.lang.Exception.
  • A java.rmi.RemoteException there is a public field called detail of type java.lang.Throwable
  • With a java.sql.SQLException you need to use the method getNextException() to get the chained java.sql.SQLException.
  • When you have an java.lang.reflect.InvocationtargetException, you can get the thrown target java.lang.Throwable using the getTargetException() method.
As usual, the best way to check how to get that piece of information is to read the documentation of the specific Exception.