From ddc997adee3509d45880bd8f028d674bda39e4e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 16 Jan 2025 09:47:25 +0100 Subject: [PATCH] Close the SQLite connection to memory database when the database has been dropped Instead of relying on the garbage collector to close the connection by deleting the object, this ensures that the memory database is actually dropped before a new connection is instantiated and keep the database. Closes #13796 (grafted from 0a2239fb548b5b20a9ae4d4a8c8c6c34cfed5683) --HG-- branch : 6.0 --- trytond/trytond/backend/sqlite/database.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/trytond/trytond/backend/sqlite/database.py b/trytond/trytond/backend/sqlite/database.py index eafd286785..3f08d66e9d 100644 --- a/trytond/trytond/backend/sqlite/database.py +++ b/trytond/trytond/backend/sqlite/database.py @@ -441,14 +441,25 @@ class Database(DatabaseInterface): return self._conn def put_connection(self, connection=None, close=False): - pass + assert connection == self._conn or self._conn is None + if self.name == ':memory:': + if (self._local.memory_database._conn is None + and connection and close): + connection.close() + self._conn = None + elif close: + connection.close() + self._conn = None def close(self): if self.name == ':memory:': - return - if self._conn is None: - return - self._conn = None + if (self._local.memory_database._conn is None + and self._conn): + self._conn.close() + self._conn = None + elif self._conn: + self._conn.close() + self._conn = None @classmethod def create(cls, connection, database_name):