Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Application crashes after my first record in Postgresql

 
alex cintea
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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 :

 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3583
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • 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.

 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic