2
我有一個連接到多個數據庫(超過400)的Django應用程序。數據庫憑據經常更改。我無法使用django數據庫支持,因爲它需要靜態設置。關閉Django的mysql連接
因此,當加載返回遊標的頁面時,我創建了動態數據庫連接。
我有以下問題:
- 這是釋放光標以適當的方式(使用析構函數__ 德爾 _)
- 如果我直接使用close()方法,是我的安全與我的析構函數
- 我應該爲連接對象做類似的事情,或者只是關閉遊標就夠了?
有沒有對此提出任何標準的解決方案?
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)