Link to home
Start Free TrialLog in
Avatar of cbirds1
cbirds1

asked on

What causes the error java.sql.SQLException: Closed Connection?

We get this error java.sql.SQLException: Closed Connection
sporadically and want to know what causes this error?

Thanks
Avatar of bobbit31
bobbit31
Flag of United States of America image

do you keep your connection object open for long periods of time and use that connection over and over?

It could be b/c your database has a timeout on open/idle connections. If time is up, then the connection will close and you will get this error.

if this is not what you are doing, please post the offending code.
Avatar of cbirds1
cbirds1

ASKER

No, we close the connection after each database call.
Avatar of cbirds1

ASKER

No, we close the connection after each database call.
...well then, you're probably closing the connection, and then trying to create another Statement on it.
Yeah, it sounds as if you've got loop issues where you accidentally close a connection when you didn't want to, or you accidentally accessed a connection that'd been closed.  Try taking out all the code that calls Connection.close() except at the VERY END of you application, (you don't want an open connection then!) and then try it.

Also, you might try wrapping every statement that uses your connection like this:

BEFORE:

Connection conn;
try
{
  conn.executeQuery( "SELECT * FROM..." );
}

AFTER:

Connection conn;
try
{
  if( !conn.isClosed() )
    conn.executeQuery( "SELECT * FROM..." );
  else
    System.err.println( "Oops!  Connection closed already!" );
}
Incidentally, you're repeating your responses because you've bookmarked the page that comes after you post the first response.  You need to:

a)  Not bookmark the page.  Just go there manually.

or

b)  Go to the page manually through the index, then bookmark THAT page.

The page you've bookmarked is a result of a GET/POST request, meaning that data is being sent in addition to the requested URI.  IE, therefore, replicates the data being sent as well, resulting in the repeated responses.
Avatar of cbirds1

ASKER

I didn't mention in my original posting that we are using Servlets. With each call to the database we do an Open and Close Connection. The Closed Connection error that we get has no pattern...we just get it sporadically when we click through our web pages.
Are you sure you're closing out the connections in the
finally clause? The reason I ask is if anuone else is writing servlets and not paying attention/doesn't know.... Are you using connection pooling and checking out a bunk connection?

1.How are you getting your Connections?

Static Connection pool, instantiate on each request, etc.?

2.I hope you have one base servlet that has a getConnection() method and that all your servlets inherit this method.

If each servlet gets a Connection on its own, then maybe the servlets that work are getting their connections properly and the one that's broken is closing the Connection prematurely.

3.Finally, if you are going to an AS/400 and you are doing updates, maybe you need to set autocommit to true, because logging is off by default.

It would be easier to help if you posted some code,...
ASKER CERTIFIED SOLUTION
Avatar of SuperKarateMonkey
SuperKarateMonkey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cbirds1:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept SuperKarateMonkey's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer