• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet causing: java.sql.SQLException: Connection closed

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's causing the error is that you're trying to use the connection after it's been closed.
 
Varnam Aayiram
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys...

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



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic