• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Closing connection - best practices

 
Maneesh Godbole
Saloon Keeper
Posts: 11312
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a stand alone desktop application, which uses embedded Java DB (formerly Apache Derby).
I have a DBManager class which talks to the DB for all the CRUD operations. Since opening connection is resource heavy, I am maintaining the connection as an instance variable.

I am wondering what is the recommended approach to closing the connection. When and where?
Options I could think of
1) Override finalize in DBManager and try to close there.
Con: No guarantee it will be called
2) My application has a UI. Provide a window listener and try to close just before calling System.exit(0);
Con: Will not always work. e.g. JVM forcefully terminated, User logout etc
3) Add a shutdownHook
Con: Can't think of anything

I am inclined to go with #3. Any other suggestions?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:I am inclined to go with #3. Any other suggestions?

When I developed my SCJD (currently OCMJD) assignment, I also used a shutdown hook to close my database. It worked very well, so that would be my choice again in this situation.
 
Maneesh Godbole
Saloon Keeper
Posts: 11312
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel.
I used the shutdownhook and its working as expected.
 
Claude Moore
Ranch Hand
Posts: 828
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... unless user kills the java process via task manager (or kill -9).
I had a sad experience with an end user killing via taskmanager a desktop app because was unable to find "Exit" item in the menu. And I'm not kidding

 
Maneesh Godbole
Saloon Keeper
Posts: 11312
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claude Moore wrote:I had a sad experience with an end user killing via taskmanager a desktop app because was unable to find "Exit" item in the menu. And I'm not kidding



Yup. I noticed that. But if you consider the OS killing the JVM, it kind of makes sense. After a considerable amount of googling, I am yet to find anything which can handle this scenario (and to be honest I do not think it is possible).
 
Scott Selikoff
author
Saloon Keeper
Posts: 4031
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about #4: Not keeping a connection open, and closing it immediately after use.
Cons: Performacne?

Why is opening a connection to an embed database so resource heavy? Also, if the db is embedded in the desktop app, what's the problem if the user kills the app without closing the connection?
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quoting from the Derby tutorial:
"
A clean shutdown performs a checkpoint and releases resources. If an embedded application doesn't shut down Derby, a checkpoint won't be performed. Nothing bad will happen; it just means that the next connection will be slower because Derby will run its recovery code.
"

Essentially when you start Derby up it does some checks based on how it was shut down. A proper shutdown results in a quicker start up. Of course, for an embedded db, creating the first connection involves actually starting up the db, which is not the same as a regular database, which will already be up and running.

So, there's no issue if the connection is killed without a proper shutdown, it just means the startup will be slower next time.
 
Claude Moore
Ranch Hand
Posts: 828
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote: So, there's no issue if the connection is killed without a proper shutdown, it just means the startup will be slower next time.

This would answer the (rare) cases when users improperly shut down an application (via task manager, unplugging their PCs and other many weird ways to create problem). A shutdown hook guarantees a proper release of resources when JVM exits, otherwise, well, you'll have to wait a bit more.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic