• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

java.sql.Statement Question

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I use java.sql.Statement.close it can throw a SQLException. I am trying to determine how to handle such an exception since I don't know under what circumstances it can be thrown or what it means in that context (assuming no previous statement exceptions). I don't know if I should treat it as a warning or a fatal error. Can someone fill in the blanks for me? TIA.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dennis,

The general approach is - cleanup stuff is done in finally block:

Now, again, this is general practice, that if SQLException is thrown during cleanup, most of the time, the error is logged and code proceeds further - i.e. it is not treated as fatal (or fatal enough to end program's operation).
The reason is - we've go exception during closing of resource, right? So, if we are not able to close a resource properly, it won't affect program severely. Again, in real life program, connection pool is used - which is opened during application startup and closed during application's ending - where it won't do much harm if in rare case there's any problem during cleanup.

However, if the behavior is consistent (i.e. we are always getting such exceptions during closing of resources), then it needs further investigation.

I'm not expert in this area, but these are just my two cents.

I hope this helps.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply and yes that is what I was looking for. It didn't seem to me that an exception on a close would/should be fatal but I didn't know what was going on behind the scenes.
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That all depends upon the context of your application and the purpose of the statement.

Can the program continue upon failure? If not, then it's a fatal error. If it can continue, what are the ramifications of the error? Is it "Oh, that failed, please try again." or is it "Well, it would have been nice if that succeeded, but no matter."? Or something else?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again. To me the only thing that makes sense is the last. I can't think of any reason why a close failure would do anything bad enough to prevent the app from running (does it prevent future connections or future uses of NEW statements?). That is the crux of my question. What possible harm, under any circumstance, would ignoring a close failure cause?
 
Bear Bibeault
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A failure to close a resource could eventually cause the whole thing to fail -- what happens if the DB runs out of connections because they weren't properly closed?

That said: close failures are rare and usually mean that something's gone awry with the network.

One problem with SQLExceptions is that it's deucedly hard to figure out what caused them. Most DB's are really bad about reporting errors. If there are some details, it's usually some passive-aggressive error like "a column had bad data". Makes you want to reach through the wire and throttle the DB "Which freaking column?".

 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In practice, I always ignore errors thrown by closing of JDBC components. ("ignore" == "log and continue".) I figure if closing a JDBC component throws an exception, that means that every other use of JDBC is going to throw an exception and the whole application is imminently about to go up in smoke.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Putnam wrote:Thanks again. To me the only thing that makes sense is the last. I can't think of any reason why a close failure would do anything bad enough to prevent the app from running (does it prevent future connections or future uses of NEW statements?). That is the crux of my question. What possible harm, under any circumstance, would ignoring a close failure cause?


Well, there are implications of that.

Consider a worst case scenario - where you are getting this exception, and you are not able to close DB connection.
Now, when a connection is opened, and you get such exception during closing a connection, its fine from your perspective - because the moment you get out of that method, Connection object is eligible for gc.

But what about actual connection which is opened at DB side? When Connection object is garbage collected, it is very much possible that actual DB connection is still open.

Now, consider such problem from few hundred, or thousand clients (i.e. application is directly connecting to DB and application is being used by thousands of users).

Of course, it is quite hypothetical and/or worst case, but my point is - like Bear said, those exceptions during cleanup are not totally ignorable.

Edit: Oops! Bear replied before me
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But would not the next connection attempt fail? In that case it would be caught by the exception on the connection attempt. That, I do always consider a fatal error.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Putnam wrote:But would not the next connection attempt fail? In that case it would be caught by the exception on the connection attempt. That, I do always consider a fatal error.


Yes. As Bear said,

Bear Bibeault wrote:what happens if the DB runs out of connections because they weren't properly closed?


And it is indeed fatal.
 
Paul Clapham
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not necessarily. The network problem which hypothetically caused the close to fail might have been transient. Or the next connection request might get a different connection from the pool, which wouldn't require any network access.

The thing is, attempting to reason about unpredictable beasts like networks isn't the same as attempting to reason about Java code. You can rarely say "Event X happened here, so event Y must happen next". All you can do is to assume that bad things can happen at any time without warning.

But anyway I think if you asked people with large-scale database systems, they would say either that they have never had an occasion where closing a JDBC component threw an exception, or that they don't know because they don't bother to monitor for that particular situation.
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic