I am getting 'SQLServerException : 0 The result set is closed. The result set is closed.@java.lang.Thread:run:722' in the following code.
I can see I am not closing the statement or resultset then why I am getting this exception.
Anyone can help?? Thanks in advance
private boolean isConnectionValid(Connection connection){
//SQL statement to execute the query.
Statement statement = null;
//resultSet receives the result of statement execution.
ResultSet resultSet = null;
//detect the connectivity.
try{
//create a statement.
statement = connection.createStatement();
//define the specific query after the statement is created.
String query = databaseType == DatabaseType.ORACLE? "select 1 from dual" : "select 1";
//apply the statement to execute the query.
resultSet = statement.executeQuery(query);
// if the resultSet.next() returns true, the connection is fine. Otherwise the connection is invalid.
return resultSet.next();
}catch(SQLException e){
//If any SQL Exception is caught, the connection is invalid as well.
Common.logException2(getLogger(), e, null);
return false;
}finally{
//finally close statement and resultSet to prevent cursor leak if any of them is not null.
Common.closeStatementAndResultSet(statement, resultSet, getLogger());
}
One example where I am using isConnectionValid method is following:
public boolean execute(Logger logger) throws SQLException {
try {
if( !query.toUpperCase().startsWith("SELECT") ) {
queryLoggerInfo(database.getDbName() + " " + this);
}
return statement.execute();
} catch (SQLException e) {
if (database.isConnectionValid(connectionId)){
//log it
} else {
// other log methods
}
throw e;
}
}