• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to handle a database connection

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
What's wrong? Where are you going? Stop! Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic