I have a python script that globally invokes a database connection object with mySQL. There is an insert function insertToTable(actionDate, action) which inserts a record to the table. The main section of the script inside if __name__=="__main__": runs as an infinite while loop for hours/days.
The problem is, the mysql connection object can go unused for many hours which results in timing out of the connection by the MySQL server. I currently use try & except within the function to catch the exception. However this leads to code repetition since I have to include a try & except block to handle this error in every function.
I would like to know if there is a mechanism in python by which I can hook insertToTable() to another function, say checkConnectivity(). When insertToTable() is invoked, first checkConnectivity() is executed and then the execution of insertToTable() continues.
You could create a decorator that calls the
checkConnectivityfunction. Example: