簡體   English   中英

是否應將同一對象用於獨立的不同數據庫操作?

[英]Should I use the same object for independent, different database operations?

class DB:
  def insert(self, data):
      #connect to database and insert the data into Table.

  def update(self, data):
      #connect to database and update the Table.

class EditInfo:
  def Insert_into_Table(self, data):
      self.object1 = DB() #created the object here
      self.object1.insert()

  def update_the_Table(self, data):
      self.object1.update() #Reusing the same object created before  

提到object1 ,最好是為一個經常執行數據庫操作的類創建一個對象(例如self.object1),並以多種方法重用它(例如在Insert_into_Table(),update_the_Table()中),而不用擔心刪除它 ?
或者,是否應該刪除調用__del__的對象,並在每次需要時創建一個新對象?

有一個非常低級的對象用於訪問數據庫,並一遍又一遍地重用是合理的。 實際上,我認為您最好有一個單例來執行數據庫訪問,並且每個會話僅創建一次。 唯一的問題是,是否應在數據庫對象和業務邏輯之間設置層(可能的答案是肯定的)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM