• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to handle a database connection

 
Scott Hibbard
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm in need of some design advise regarding handling a database connection for a small GUI.

When/how often should you open and close the database file? It seems terribly inefficient to open and close the database every time you need to read or write to it. So far I'm trying to open it once and close it once on exit. Does that sound like the way to go? Are there any downsides to that approach?

Assuming that's the way to go, how do you go about doing this? Right now I have the following code in main:

Here's PersonListView



I'm using a very simple MVC pattern. PersonListView handles the swing components including a JFrame to hold it all together. I'd like the database to be available until the window is closed which it's not.

Can someone explain why db.close() is reached before the window is closed? I'm assuming some new threads are kicked off somewhere in there but I'm a little fuzzy on the details.

The big question I'm looking for is this: How do I gracefully handle resources like a database in a locally run swing app - specifically on close? Should I create a helper class to handle the connection? Where do I put code to handle the cleanup when the window is closed?

Thanks in advance for any and all help!
 
Scott Hibbard
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nevermind. I figured it out. :-)

I changed the default close operation to do nothing and added a Window Listener to listen for a windowClosing event. Using the listener, I was able to close the database, dispose of my frame and exit the program.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic