This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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???
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?
Joined: Jul 06, 2009
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???
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.
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?