|
-
November 21st, 2009, 03:55 AM
#1
[RESOLVED] how to disconnect to database in this case?
I'm working with JDBC.
1) I want to disconnect to database in a destructor because my class uses the connection during execution time. Please give me a hand where I should put my disconnectToDatabase() method.
2) I googled for that and I used finalize() method but when I closed my frame, it didn't show the message dialog. I think there is memory leak.
So in fact, when is finalize() method invoked? How to disconnect to database in this case?
Code:
public class LeftPane extends JPanel
{
private DatabaseLoader databaseLoader;
public LeftPane()
{
try {
databaseLoader = new DatabaseLoader();
} catch (ClassNotFoundException e) {
System.err.println("Have no a driver");
}
...
// connect to database
try {
databaseLoader.connectToDatabase();
...
} catch (SQLException e) {
System.err.println("Database error");
}
...
}
protected void finalize() throws Throwable
{
JOptionPane.showMessageDialog(null, "End"); // it doesn't show when I close my app.
// disconnect to database
databaseLoader.disconnectToDatabase();
super.finalize();
};
}
Thank you.
Last edited by Emerald214; November 21st, 2009 at 04:04 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|