Oh, yes, this is what I was looking for but could not find! :) Thank you, dwblas!
Strangely, I haven't heard of __del__ since I first learn some python (version 1.6, I guess).
In the meantime, I found there is one more way in Swing, and that is Runtime.addShutdownHook(Thread).
I defined this function:
-
class ShutdownTask( java.lang.Thread ):
-
def __init__( self, connection ):
-
self.conn = connection
-
print 'ShutdownTask setup: object = %s' % connection
-
def run( self ):
-
print 'ShutdownTask in progress'
-
if self.conn is not None:
-
try:
-
print 'ShutdownTask: shutting down database connection'
-
self.conn.conn.close()
-
print 'ShutdownTask: connection closed'
-
except Exception, e:
-
print 'ShutdownTask: exception'
-
print e
-
print 'ShutdownTask: done'
-
and in the init thread I call this:
-
Q = Connection() # my class to represent database connection
-
shutdownTask = ShutdownTask( Q )
-
java.lang.Runtime.getRuntime().addShutdownHook( shutdownTask )
-
Works perfectly and is "Swing native", but __del__ in the main GUI class should do the same job.