Jun-30-2017, 09:37 PM
I've created a separate Python file with a class stored in it. I'm using the mysql.connector module to perform my mysql connection and queries. This is how the class is setup at the moment:
I did a bit of Googling but haven't been able to find an answer to this. Can any of you guys help?
Thanks,
Isaac
class sqlConnection(): def __init__(self): #SQL Connection variables sqlServer = '' sqlUsername = '' sqlPassword = '' sqlDatabase = '' #Establish connection to SQL server try: con = mysql.connector.connect(host=sqlServer,user=sqlUsername,password=sqlPassword, database=sqlDatabase) print "Connection to SQL server success: %s, %s" % (sqlServer, sqlDatabase) except: print "Connection to SQL server failed: %s, %s" % (sqlServer, sqlDatabase) def exit_handler(self): #Close connection to SQL server is server closed self.con.close() print "Connection closed to SQL server: %s" % (sqlServer)The class is imported in my main Python file like so:
import sqlConnection sqlHandler = sqlConnection.sqlConnection()My worry is that when the application closes, the SQL connection will remain open. I've tried using the exit_handler() function within the class to close the connection, but that never seems to be called when the application closes.
I did a bit of Googling but haven't been able to find an answer to this. Can any of you guys help?
Thanks,
Isaac