I have a django application that connects to multiple databases (more than 400). Database credentials change often. I cannot use django database support because it needs static setup.
So I create dynamic database connection when loading the page that return a cursor.
I have the following questions:
- Is this an appropriate way to release a cursor (using destructor __del_ )
- If I use the close() method directly, am I safe with my destructor
- I should do something similar for the connection object, or just closing cursor is enough?
Is there any proposed standard solution for this?
class MyDatabaseManager(object): def __init__(self, database_name): self.cursor = None #for safety in case next command return an exception self.cursor = some_object.get_cursor(database_name) def close(self): self.cursor.close() self.cursor = None def __del__(self): if not (self.cursor is None): self.close() def execute_query(self, sql_query, parameter_list): return self.cursor.execute(sql_query, parameter_list)