• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is the connection to the data base cossed properly.

 
Jaidev Arer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is one of the function to update in DAO. Can you please go through the code and let me know whether the database connection is closed properly or not. And also let me know if there is any changes required in code.




Thanks for your valuable time.
 
vijin das
Ranch Hand
Posts: 129
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaidev Arer wrote:Below is one of the function to update in DAO. Can you please go through the code and let me know whether the database connection is closed properly or not. And also let me know if there is any changes required in code.




Thanks for your valuable time.

The code snippet you have given is incomplete ...
you are calling getconnection() to get the connection ... is it a static method ?
you should have used if (con != null) con.close(); instead of con.close() and for for closing you prepared statement object also...
and why you are returning null ??? ...
 
Jaidev Arer
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijin,

getconnection() is a static method.

Change the return type to void and removed the return null statement.

Is there method or function to call to close all the opened database connection explicitly?

Thanks again
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is there method or function to call to close all the opened database connection explicitly?

No. But then if you are opening a connection you are presumably closing it once you are finished with it?

 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Closing p.close() in two places is unnecessary... Its enough to be retained in "finally" block itself since that block will be executed allegedly before getting out of the method. so guaranteed, connections are closed even an exception occurs or this update transaction is success...

Instead of returning String, return a "success" return code... you can use integers to indicate the result...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic