?
Solved

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

Posted on 2003-02-21
13
Medium Priority
?
1,060 Views
Last Modified: 2012-06-21
We get this error java.sql.SQLException: Closed Connection
sporadically and want to know what causes this error?

Thanks
0
Comment
Question by:cbirds1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +4
13 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 7994332
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
 

Author Comment

by:cbirds1
ID: 7994351
No, we close the connection after each database call.
0
 

Author Comment

by:cbirds1
ID: 7994371
No, we close the connection after each database call.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:jerelw
ID: 7994406
...well then, you're probably closing the connection, and then trying to create another Statement on it.
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 7994671
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
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 7994684
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
 

Author Comment

by:cbirds1
ID: 7995312
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
 
LVL 2

Expert Comment

by:sfotex
ID: 7997477
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
 
LVL 3

Expert Comment

by:jerelw
ID: 7999926
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
 
LVL 2

Accepted Solution

by:
SuperKarateMonkey earned 600 total points
ID: 8009224
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
 

Expert Comment

by:CleanupPing
ID: 9059163
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
 
LVL 7

Expert Comment

by:tomboshell
ID: 9582211
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question