Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Application crashes after my first record in Postgresql

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an application that crashes after my first attempt to insert a record in PostgreSQL. Everything works Ok, but if I want to insert another record my program crashes.

I don't have any SELECT * within my queries. Any help will be appreciated.

That's the error I receive :

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error says it all, the connection is already closed when you try to insert data.

How is your code set up?
 
alex cintea
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:The error says it all, the connection is already closed when you try to insert data.

How is your code set up?




Method selectEaters()


Method insertEaterDB()


DatabaseConnection.java

 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I noticed when you do


You didn't assign it to a Connection variable. This makes the "connection" variable in say connection.createStatement() or connection.prepareStatement() complain because the "connection" variable is null.

If you are able to get a non-null connection then in the finally block, you should pass back the connection variable to the DatabaseConnection.closeConnection() method so that close method closes the specified opened connection. (too much "connection" in a sentence)
 
alex cintea
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tsank Thanks for your explication!


So is it alright if I do something like that ?

It works alright with this modification but I would like to know if it is a reliable solution.

Thanks!

 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So did you managed to solve your first problem?

Reliable? Not really. The return in the try would leave the db connection open.

 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would also suggest not to "swallow" exceptions silently. Look at this:

If an exception occurs, it is printed to the standard output, where someone might read it (or not). However, since the catch clause doesn't throw another exception, the program flow continues, in your case by returning false. So, your method returns false in two cases:

  • there isn't the required eater in the database,
  • there was an error trying to obtain the eater from the database.

  • However, these are vastly different situations, and the caller of your method has no way to tell them apart.

    Generally, if an unexpected error occurs, it is best to allow the software to crash. This will definitely bring someone's attention to the problem, which can then be mended. If you allow your program to continue after an exception occurs, it is possible for a problem to go unnoticed for month, usually causing the application to generate wrong outputs all that time. It is unpleasant having to explain to your boss why your system crashed at the busiest time of the day, but it much more unpleasant having to explain that all monthly numbers for pas six months are wrong.

    Only when the exception is anticipated and properly dealt with it is advisable to continue the flow of the application. In all other cases the best option is to simply throw an exception from all catch clauses (checked or unchecked, depending on your project's exception handling conventions) and have it dealt with at the topmost level. In really simple applications, this let the application crash. In more complicated ones there usually is (or at least should be) some sort of reporting system which will alert someone to the problem.
     
    The only thing that kept the leeches off of me was this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/t/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic