File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes reasons for error: SQLException: No operations allowed after connection closed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "reasons for error: SQLException: No operations allowed after connection closed?" Watch "reasons for error: SQLException: No operations allowed after connection closed?" New topic
Author

reasons for error: SQLException: No operations allowed after connection closed?

sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear,
I have this error:



but I did'nt close any connection!
is there any other reason for throwing this error?

thanks,
Sahar.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

Most likely you did close the connection somewhere along the line. To check this out, use the database management tool provided with the database to monitor your open connections. You might have to use a debugger, with a breakpoint at the close() method to determine where the connection get closed.

If that does not help, also provide the following:

What type of app are you running? Simple Java? Web app?

What database?

The source code in question.


JBoss In Action
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
Dear,
Is that right if i use tha same name for pstmts and connections of all my queries ? even when they are being used by different classess for different porposes(BY the way Im not sure but I think this classess are being executed parallel because the output shows me some result from one class and then some lines from the other class and some from the first class!!).
So is that possible that while Im using one of my connection, closing another connection make the problem? (be cause they are using the same name!?)
e.g. what is happening here, is that some times,e.g. : I have 4 connection and 4 pstmt in different classes. two for delete query and 2 for select query . it happens that I closed one of my connection after executing delete query while the other one is still going to select (please mention that they have named similarly), in this moment. Is that possible that connection.close which is for closing delete query, close the connection of select query that is still in use???

thank you in advance,
Sahar.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

Not sure if I follow all of that. When you open a connection you get a java.sql.Connection object returned. From that Connection object you create Statement and PreparedStatement objects. Once you close that Connection object, you can no longer use the Statement or PreparedStatement object created from it.

If you create multiple Connection objects, then you have to keep track of which Statement and PreparedStatement objects belong to which connection. If you close one of the Connection objects, only its Statements and PreparedStatements become unusable - the Statements and PreparedStatements created from the other connections are still usable.

At least, that is how it is supposed to work. Though the vendor could always implement the classes incorrectly. What database are you using? Have you used any database tools to monitor that actual number of connection?
sahar sa
Ranch Hand

Joined: Jul 06, 2009
Posts: 102
dear,
I am using java netbeans+ jade(is a middleware to support agents)+mysql server 5.
My question is If I have a db and I use the same name for several connections what will happen when I close one of the connections? All th econnections will be closed or one? if one, how it recognise that which connection is supposed to be close?
to elaborate I give you an example:
I have one select and one delete query as below:




using this closing and gettin connection methods:



e.g:If in one class I call select method( which will make a connection and I keep it in variable "con") and during selection from DB, another class calls method delete (which will make another connection named "con" ). please mention that now we have 2 connection both named "con" one for select and the other one for delete. now what will happen if method delete finish executing, and after, I close the connection by calling con.close() ?
this will only close my connection that refers to delete or conecction from select will be closed also???

sorry for long post and thank you so much!
sahar.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

Here is your problem - you wrote a "getConnection" convenience method which, on the first call gets a connection and on every call after that returns the exact same connection. So there is only one open connection in your code, and when it is closed none of your code can make any more calls. What is worse, the next piece of code that asks for a connection will get the closed connection object and be unable to do anything with it.

By the way, this has nothing to do with calling the connection variable "con" in multiple locations.
Kanika Sud
Greenhorn

Joined: May 09, 2011
Posts: 21
I have a very similar problem.

My connection is created in a singleton class and then I use it wherever I want as follows (in the constructor of a client class, say):




And now, if I close the connection it is no longer accessible. And as far as I can see it, there is no method to open a connection after it is closed. Or is there? And if donot close the connection across classes that use it, is it a good design practice?

Thanks,
Kanika.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reasons for error: SQLException: No operations allowed after connection closed?