aspose file tools*
The moose likes JDBC and the fly likes Servlet causing: java.sql.SQLException: Connection closed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Servlet causing: java.sql.SQLException: Connection closed" Watch "Servlet causing: java.sql.SQLException: Connection closed" New topic
Author

Servlet causing: java.sql.SQLException: Connection closed

Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi Guys...

I coded a servlet that seem to be working the way I want it to, in terms of writing to the database. However when I come back to look at the stack trace, I find some errors, despite the the application functioning the way I intended it to.

Please take a look at the following block of code. In the criteria2Request method exists a while loop. The while loop has an if statement. If the if condition is met, the method 'slotTaken' is called which outputs a jsp page( request.getRequestDispatcher("bookingExceded.jsp").forward(request, response); ). If for the duration of the while loop the if condition is not met, than a series of setmethods from a prepared statement are executed, outside the while loop.

When the if condition is not met and the while loop runs itself out and the code outside the loop gets to be executed, there is no error in the stack trace. When the if condition is met there seem to be errors in the stack trace, and I suspect this is when the code after the while loop is executed, after meeting the condition inside the if. I am not sure what is causing this error. Below is the code and error mentioned in the stack trace. I really hope someone can advise. Thanks.



Stack trace error:


and it 'points' to the following lines...


hope someone can advise...thanks guys...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What's causing the error is that you're trying to use the connection after it's been closed.
Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi David..

Thanks for the response, I appreciate it. Yes I had a suspicion that it was because the connection was closed earlier. Why I did that was because if I didn't do it, than an entry will still be made in the booking table, as the rest of the code after the while block will still execute after the block runs itself out.

Anyway, now I have restructred the code and have managed to eliminate the SQl error, but am now faced with two new problems. I am getting this error: java.lang.IllegalStateException: PWC1227: Cannot forward after response has been committed" and also an entry is made in the booking table, depite the if condition being met. What I intended was for the slotTaken method to be called once the if condition is met, which means the 'bookingNotConfirmed.jsp' will be output and no entry will be made to the booking table. However what is happening is that, the 'bookingNotConfirmed.jsp' is being output, which is fine, but an entry is being made in the booking table. My suspicion is that the 'bookingConfirmed' method is being called after the while block is over, hence the writing to the table. I am posting the code below. I am not sure how to rectify this situation, hope someone can advise. Thanks...

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Varnam Aayiram wrote: My suspicion is that the 'bookingConfirmed' method is being called after the while block is over, hence the writing to the table.

Of course it is--it's the last statement in the method, clearly outside of the while and try/catch block.

There's no need to "suspect": you have the code in front of you.

With all the crazy indentation it's much harder to read your code; I suspect you're mixing tabs and spaces: I'd recommend picking one and sticking with it.
Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi...

Thanks for the response. Where else do I place my call to the booking confirmed method? If I place it within the while loop, as in the last statment in the loop, the bookingconfirmed method is called twice ,bypassing the slotTaken method. I am sorry about the bad code indenting. I have improved the indending and am re-posting the code. I really hope someone can help. Thanks.



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You only want to book it if the slot isn't taken already, right? So check to see if it's taken: if it's not, call the method.
Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi David.

Thanks for the response. Yes, I am checking...the while loop goes through the table and the if condition sees if the user's input values match any existing record in the table ( if(unique1.equals(unique2)) ) and when that is met, I am calling the slotTaken method. If the if condition is not met, it means there is no simillar booking, meaning the slot is free, so I than call the bookingConfirmed method.

The problem I am having is the 'bookingConfirmed' method is getting called even after the if condition is met and the 'slotTaken' method is called. How should I place the call to the 'bookingConfirmed' method in such a way that this dosen't happen?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Think it through: I'm not just going to give you a complete answer (please see NotACodeMill). Can't you keep track of whether or not the slot's been taken and if it hasn't been, call the method?
Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
David Newton wrote: Can't you keep track of whether or not the slot's been taken and if it hasn't been, call the method?


Yes I think I am doing that...
Varnam Aayiram wrote:
...the while loop goes through the table and the if condition sees if the user's input values match any existing record in the table ( if(unique1.equals(unique2)) ) and when that is met, I am calling the slotTaken method. If the if condition is not met, it means there is no simillar booking, meaning the slot is free, so I than call the bookingConfirmed method.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If the method is being called no matter what then you're either (a) *not* keeping track, or (b) not calling the method if and only if the slot isn't taken.
Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi Guys...

Sorry for the delay in posting the solution, a 'return' statement solves the problem. Following is the code from the 'try' block:



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet causing: java.sql.SQLException: Connection closed