From dbccfea27a7abb07727b47973a5a6caaaaff0ec6 Mon Sep 17 00:00:00 2001
From: Marko Mecina <marko.mecina@univie.ac.at>
Date: Mon, 31 May 2021 12:01:13 +0200
Subject: [PATCH] get rid of class-owned SQL session instance

---
 Tst/codeblockreusefeature/codeblockreuse.py | 10 +++++-----
 Tst/codeblockreusefeature/db_interaction.py | 21 +++++++++++++--------
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/Tst/codeblockreusefeature/codeblockreuse.py b/Tst/codeblockreusefeature/codeblockreuse.py
index 95da74d..cd03145 100755
--- a/Tst/codeblockreusefeature/codeblockreuse.py
+++ b/Tst/codeblockreusefeature/codeblockreuse.py
@@ -257,8 +257,9 @@ class CBRSearch(Gtk.Box):
         super().__init__(*args, **kwargs)
         self.app_win = app_win
         self.logger = logger
-        with db_schema.session_scope() as session:
-            self.session = session
+
+        # with db_schema.session_scope() as session:
+        #     self.session = session
 
         self._filter_searchstring = ''
         self._filter_type_snippet = None
@@ -769,11 +770,10 @@ class CBRSearch(Gtk.Box):
         Loads the data from the database. The filter properties are used to build the SQL query.
         """
         self.logger.debug('Loading data from the database')
-        self.session.commit()  # this updates the session instantiated in the class init
         if self.filter_searchstring != '':
-            self.data = db_interaction.query_using_textsearch(session=self.session, expressions=self.filter_searchstring)
+            self.data = db_interaction.query_using_textsearch(expressions=self.filter_searchstring)
         else:
-            self.data = db_interaction.query_get_all_entries(self.session)
+            self.data = db_interaction.query_get_all_entries()
         self.load_data_into_liststore(self.data)
 
     def reload_data(self):
diff --git a/Tst/codeblockreusefeature/db_interaction.py b/Tst/codeblockreusefeature/db_interaction.py
index 9a9412f..62b1e9c 100644
--- a/Tst/codeblockreusefeature/db_interaction.py
+++ b/Tst/codeblockreusefeature/db_interaction.py
@@ -2,8 +2,10 @@ import db_schema
 from db_schema import CodeBlock
 
 
-def query_get_all_entries(session):
-    data = session.query(CodeBlock).all()
+def query_get_all_entries():
+    with db_schema.session_scope() as session:
+        data = session.query(CodeBlock).all()
+        session.expunge_all()
     return data
 
 
@@ -27,16 +29,19 @@ def delete_db_row(id):
     return
 
 
-def query_using_textsearch(session, expressions):
-    data = session.query(CodeBlock).filter(CodeBlock.description.contains(expressions)).all()
+def query_using_textsearch(expressions):
+    with db_schema.session_scope() as session:
+        data = session.query(CodeBlock).filter(CodeBlock.description.contains(expressions)).all()
+        session.expunge_all()
     return data
 
 
-def query_code_types(session):
-    data = session.query(CodeBlock.code_type).group_by(CodeBlock.code_type).all()
+def query_code_types():
+    with db_schema.session_scope() as session:
+        data = session.query(CodeBlock.code_type).group_by(CodeBlock.code_type).all()
+        session.expunge_all()
     return data
 
 
 if __name__ == '__main__':
-    with db_schema.session_scope() as session:
-        query_code_types(session)
+    query_code_types()
-- 
GitLab