This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

ResultSet closed all the time

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an application using four threads. In each of the 4 threads, an SP is called which returns a resultset. When i use this result set it returns an SQL exception saying that the resultset is closed.

In the code, Once I complete the process i give Thread.yield(). But still it is giving me a problem. I am not explicitly closing the resultset anywhere. What am I supposed to do???

Regards.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't quite guess from the description. Can you post some code around the connect, call the SP, use the result set?
 
Sheriff
Posts: 26963
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possible that you are closing the Statement that produced the ResultSet. Or it is possible that you are reusing it to produce another ResultSet. Either of those two things would cause your problem. I am sure there are other ways to do it, too.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using Connection, Statement or ResultSet object as an instance variable? or is it local?

It could also be because of race condition between different threads.

Can you post the class which actually gets the resultset from stored procedure and the overridden run() method.

Have you tested the same stored procedure by single thread like from java program. If still you are not getting resultset, then I think you should check your stored procedure first.

Thanks

Naseem
[ June 30, 2006: Message edited by: Naseem Khan ]
 
Naresh Rajan
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naseem : The code works fine when there is only one thread. So there is no problem in the SP.

This is the code which establishes the connection and uses the resultSet :
//Code starts
public class ITDDatabaseManager {

private static CallableStatement callable4 = null;
private static ResultSet resultSet = null;
private Connection conn = null;


public void doConnection() {
//this method establishes connection with the database
}

public int getDownloadData(String filename, String arg0, int fileType) {

try{
//Check whether the DB connection
if (conn==null || conn.isClosed()){
doConnection();
}

//Prepare callable statement
callable4 = getCallableStmt(case1);

//register all the inputs and outputs
registerAllParams(case1);

//Execute the Sp
callable4.execute();

//get the outputs and check for errors
succInd = callable4.getInt(3);
retVal = callable4.getInt(4);
resultSet = callable4.getResultSet();
if (succInd == 0 && retVal == 0){
//There is no error

}
while(resultSet!=null && resultSet.next()){
//do the operation meant to be done
}

}catch(Exception e){

}

//Code ends


Regards
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are the statement and the result set static fields?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using one statement instance for all Threads ,whenever you execute the statement, you close automatically the previous ResultSet which it holds on it.Because a statment instance can hold only one ResultSet.
Reexecuting same statement will interrupt (close) the previous resultset.
[ July 01, 2006: Message edited by: sinasi susam ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naresh,
As Ilja Preuss pointed out, why you have declared connection, resultset etc. as static variable. When there will be more than one thread executing your method, then there will be a race condition between the thread.

You should not declare resultset, connection, callablestatament as global things neither static nor instance.

I have made some changes to your code. Try this in place of yours.



Regards


Naseem
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic