• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1067
  • Last Modified:

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
0
cbirds1
Asked:
cbirds1
  • 3
  • 3
  • 2
  • +4
1 Solution
 
bobbit31Commented:
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.
0
 
cbirds1Author Commented:
No, we close the connection after each database call.
0
 
cbirds1Author Commented:
No, we close the connection after each database call.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jerelwCommented:
...well then, you're probably closing the connection, and then trying to create another Statement on it.
0
 
SuperKarateMonkeyCommented:
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!" );
}
0
 
SuperKarateMonkeyCommented:
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.
0
 
cbirds1Author Commented:
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.
0
 
sfotexCommented:
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?

0
 
jerelwCommented:
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,...
0
 
SuperKarateMonkeyCommented:
It's almost invariably a bad idea to open and close the Connection every time you call the service() method of a servlet, (or doGet(), doPost(), doXxx() etc...)  I mean, that's why the java designers invented the init() and destroy() methods.  There's a ton of overhead that you run into opening and closing connections.

Why on earth wouldn't you just open the connection in init() and close it in destroy()?  If you can't answer that question adequately, I'd strongly suggest you do just that.
0
 
CleanupPingCommented:
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.
0
 
tomboshellCommented:
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
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now