From 6fb5700f9498fbe85657eaf0624dc1984c202a16 Mon Sep 17 00:00:00 2001 From: Nicolas Sebrecht Date: Sat, 9 Apr 2016 19:52:33 +0200 Subject: [PATCH] SQLite: close db when done Signed-off-by: Nicolas Sebrecht --- offlineimap/accounts.py | 1 + offlineimap/folder/LocalStatus.py | 3 +++ offlineimap/folder/LocalStatusSQLite.py | 8 +++++++- offlineimap/repository/Base.py | 8 ++++---- offlineimap/repository/LocalStatus.py | 12 +++++++++--- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py index c40820b..c9255c3 100644 --- a/offlineimap/accounts.py +++ b/offlineimap/accounts.py @@ -595,3 +595,4 @@ def syncfolder(account, remotefolder, quick): for folder in ["statusfolder", "localfolder", "remotefolder"]: if folder in locals(): locals()[folder].dropmessagelistcache() + statusfolder.closefiles() diff --git a/offlineimap/folder/LocalStatus.py b/offlineimap/folder/LocalStatus.py index 2a16e0b..d9b9b70 100644 --- a/offlineimap/folder/LocalStatus.py +++ b/offlineimap/folder/LocalStatus.py @@ -154,6 +154,9 @@ class LocalStatusFolder(BaseFolder): self.readstatus(cachefd) cachefd.close() + def closefiles(self): + pass # Closing files is done on a per-transaction basis. + def save(self): """Save changed data to disk. For this backend it is the same as saveall.""" diff --git a/offlineimap/folder/LocalStatusSQLite.py b/offlineimap/folder/LocalStatusSQLite.py index 6eb549f..306ca18 100644 --- a/offlineimap/folder/LocalStatusSQLite.py +++ b/offlineimap/folder/LocalStatusSQLite.py @@ -221,6 +221,12 @@ class LocalStatusSQLiteFolder(BaseFolder): self.messagelist[uid]['labels'] = labels self.messagelist[uid]['mtime'] = row[2] + def closefiles(self): + try: + self.connection.close() + except: + pass + # Interface from LocalStatusFolder def save(self): pass @@ -329,7 +335,7 @@ class LocalStatusSQLiteFolder(BaseFolder): def savemessageslabelsbulk(self, labels): """ Saves labels from a dictionary in a single database operation. - + """ data = [(', '.join(sorted(l)), uid) for uid, l in labels.items()] self.__sql_write('UPDATE status SET labels=? WHERE id=?', data, executemany=True) diff --git a/offlineimap/repository/Base.py b/offlineimap/repository/Base.py index a2fd403..dcbb664 100644 --- a/offlineimap/repository/Base.py +++ b/offlineimap/repository/Base.py @@ -170,7 +170,7 @@ class BaseRepository(CustomConfig.ConfigHelperMixin, object): to infinite folder creation cycles.""" if not self.get_create_folders() and not dst_repo.get_create_folders(): - # quick exit if no folder creation is enabled on either side. + # Quick exit if no folder creation is enabled on either side. return src_repo = self @@ -178,7 +178,7 @@ class BaseRepository(CustomConfig.ConfigHelperMixin, object): dst_folders = dst_repo.getfolders() # Do we need to refresh the folder list afterwards? src_haschanged, dst_haschanged = False, False - # Create hashes with the names, but convert the source folders + # Create hashes with the names, but convert the source folders. # to the dest folder's sep. src_hash = {} for folder in src_folders: @@ -191,14 +191,14 @@ class BaseRepository(CustomConfig.ConfigHelperMixin, object): # Find and create new folders on src_repo. for src_name_t, src_folder in src_hash.iteritems(): - # Don't create on dst_repo, if it is readonly + # Don't create on dst_repo, if it is readonly. if not dst_repo.get_create_folders(): break if src_folder.sync_this and not src_name_t in dst_folders: try: dst_repo.makefolder(src_name_t) - dst_haschanged = True # Need to refresh list + dst_haschanged = True # Need to refresh list. except OfflineImapError as e: self.ui.error(e, exc_info()[2], "Creating folder %s on repository %s"% diff --git a/offlineimap/repository/LocalStatus.py b/offlineimap/repository/LocalStatus.py index fc34a55..5d33cdc 100644 --- a/offlineimap/repository/LocalStatus.py +++ b/offlineimap/repository/LocalStatus.py @@ -46,6 +46,9 @@ class LocalStatusRepository(BaseRepository): # self._folders is a dict of name:LocalStatusFolders() self._folders = {} + def _instanciatefolder(self, foldername): + return self.LocalStatusFolderClass(foldername, self) # Instanciate. + def setup_backend(self, backend): if backend in self.backends.keys(): self._backend = backend @@ -87,19 +90,22 @@ class LocalStatusRepository(BaseRepository): return # bail out in dry-run mode # Create an empty StatusFolder - folder = self.LocalStatusFolderClass(foldername, self) + folder = self._instanciatefolder(foldername) folder.save() + folder.closefiles() # Invalidate the cache. self.forgetfolders() def getfolder(self, foldername): - """Return the Folder() object for a foldername.""" + """Return the Folder() object for a foldername. + + Caller must call closefiles() on the folder when done.""" if foldername in self._folders: return self._folders[foldername] - folder = self.LocalStatusFolderClass(foldername, self) + folder = self._instanciatefolder(foldername) # If folder is empty, try to import data from an other backend. if folder.isnewfolder():