Results 1 to 9 of 9
  1. #1
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Question addShutdownHook with Database connection

    Hello,

    I'm working on a library to abstract away the suddleties of various databases when working with java. I have a library of various jdbc drivers and i have code that allows each to connect, disconnect from the relative database based on connection info gotten from a properties file.

    Now i'm wanting to abstract away having to worry about the connection state to the relative database so that the user of the library only has to issue a simple command such as:

    Java Code:
    MariaDB statement = new MariaDB();
          statement.prepare("SELECT * FROM table");
          statement.execute();
    the code will intelligently check if there is an existing database connection open to the MariaDB database and if it is not open, go open a new connection, if it is open, execute the statement, etc.

    My current hurdle with something like this is properly ensuring the database connections are closed and resources released upon program execution completion (end of main()).

    I looked a bit into using addShutdownHook to "register" the closeConnection() code to run as the main() is ending.. hopefully properly releasing the database resources and closing the connection. Is this an advisable route to go? or is there a better (and/or safer) way to go about accomplishing this? Keep in mind the addShutdownHook would be set in a different class/method than the current main() is running from since this is to be included in a library.

    Thanks for any insight!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: addShutdownHook with Database connection

    Most (if not all) Connections close themselves off when they are garbage collected.
    Assuming the application closes in a normal way then these will be closed.

    I would be more concerned about ensuring result sets are closed during an applications run. That's a fairly classic resource leak if that isn't handled correctly.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: addShutdownHook with Database connection

    thanks Tolls!

    I am trying to ensure that the db connection is closed even if a user of my library crashes their application somehow... such as a non-normal exit (code 1 for example). I want to have it always close the database connection, which should in turn close any PreparedStatement and ResultSet's associated with the connection (i think).

    In my library -- a user could open a new result set, work with it, and explicitly close it after they are finished working with it... however for the lazy or careless programmer, I want to "help" them close the resource if they left if open somehow.

    I can abstract the ResultSet by encapsulating it in my library somehow... but then this brings me back to the "do i use addShutdownHook to close this as main() ends?" Or is there a better way... basically I'm trying to make it so that a complete "noob" can use the library and work with databases in java without having to understand how it "works"... meaning I can't assume the user knows to close result sets or prepared statements, etc....

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: addShutdownHook with Database connection

    If the app does not exit normally (and "normally" includes crashes via exceptions) then there is nothing you can do.
    The shutdown hook won't run either.

    When I talk about the result set, it's more a case of whatever you construct should not make people think passing around result sets is a good idea, because in those situations people tend to forget to close them. If that happens a lot during a run of the program you will find the database running out of cursors (the things that point to rows in a result set).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: addShutdownHook with Database connection

    I don't' really see what I can do to prevent a determined user from passing a result set around, except ensure it gets closed if program terminates normally (via shutdown hook). Of course I won't be encouraging the practice as this gets messy quick (based on experience!).

    I could (and just brainstorming out loud here) encapsulate the result set in a method from my library, then have the result set get parsed and return an ArrayList or something that has the results in it... this would allow my library to close the resultset immediately after use - therefore it wouldn't leave open a result set ever (unless the user gets clever and goes out of their way to get a result set from the query).

    If the program does not terminate normally, then I suppose most connections would be closed automatically (along with their resources). So I understand correctly, shutdown hook cannot/will-not run as in like a try/catch/finally block to where I can "gracefully" unload resources upon a crash?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: addShutdownHook with Database connection

    You can't prevent a user from misusing the tools, and that's sort of my point.
    You are attempting to simplify JDBC, but there are some things that can't really be simplified.
    If the user does not close of result sets then they will run out of resources on the server, quite quickly depending on the application.

    Providing a predone List of the results is one solution, though you do hit the potential problem of memory. What do you do about large results?
    Anyway, that's more "things to think about" than anything.

    As for the shutdown.
    Your app can exit in one of two ways.
    The first is "normally", which means the JVM has control over how it shutsdown. In that situation it will finalize any remaining objects in the heap, which in the case of connections means closing them. SO a shutdown hook isn't needed here.
    The other is an abnormal exit, in which the JVM dies (or the computer dies). In that case, no clean shutdown is done, so no shutdown hook can be called anyway.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: addShutdownHook with Database connection

    Tolls you provide good "food for thought". thanks.

    I will contemplate the result set dilemma...

    As for the shutdown, you've prompted another question:

    when the JVM finalize the remaining objects in the heap (say for example my database connection object) -- does it simply destroy the object, or does it call the connection.close() and then destroy the object? I'm only asking as there "might" be some database connection that runs some routine on the db server side when close() is called (like some cleanup or something...maybe). in that scenario, finalize of the connection object would not result in a "clean" closure of the connection... is there a way to tell the JVM to run close() as its finalizing my object, or is this not needed or not necessary?

    It seems I was barking up the wrong tree (so-to-speak) about using a shutdown hook as it is not needed at all in either scenario.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: addShutdownHook with Database connection

    Almost all concrete Connection classes implement finalize() that will close the physical connection to the database.
    I say "almost" since there might be some noddy implementation somewhere that doesn't, but any sensible one does.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: addShutdownHook with Database connection

    thanks tolls, great information!

Similar Threads

  1. connection to database
    By manojkumarsahu in forum New To Java
    Replies: 1
    Last Post: 08-06-2010, 09:26 PM
  2. connection to database
    By buston01 in forum JDBC
    Replies: 4
    Last Post: 02-21-2010, 01:32 AM
  3. getting database connection
    By ravidasineni in forum AWT / Swing
    Replies: 0
    Last Post: 11-27-2009, 04:33 AM
  4. getting database connection
    By ravidasineni in forum AWT / Swing
    Replies: 1
    Last Post: 11-22-2009, 02:01 AM
  5. Database Connection
    By CompleteBeginner in forum New To Java
    Replies: 2
    Last Post: 05-24-2008, 02:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •