From f0a248069ccee325cb29d6de30764c79e7aae05b Mon Sep 17 00:00:00 2001 From: Mate Farkas Date: Wed, 7 Dec 2022 15:14:09 +0100 Subject: [PATCH] Allow to change autocommit attribute If autocommit changed, it was not propagated to SqliteMultithread, since its autocommit value was set only in initialization phase. According to the documentation we can temporarily disable autocommit if we need performance, but the performance was still the same as with keeping autocommit enabled. --- sqlitedict.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/sqlitedict.py b/sqlitedict.py index 3e65724..132ed7e 100755 --- a/sqlitedict.py +++ b/sqlitedict.py @@ -81,6 +81,7 @@ def reraise(tp, value, tb=None): # _REQUEST_CLOSE = '--close--' _REQUEST_COMMIT = '--commit--' +_REQUEST_AUTOCOMMIT = '--autocommit--' _RESPONSE_NO_MORE = '--no more--' # @@ -210,7 +211,7 @@ def __init__(self, filename=None, tablename='unnamed', flag='c', # See https://github.com/RaRe-Technologies/sqlitedict/pull/113 self.tablename = tablename.replace('"', '""') - self.autocommit = autocommit + self._autocommit = autocommit self.journal_mode = journal_mode self.encode = encode self.decode = decode @@ -231,6 +232,15 @@ def __init__(self, filename=None, tablename='unnamed', flag='c', if flag == 'w': self.clear() + @property + def autocommit(self): + return self._autocommit + + @autocommit.setter + def autocommit(self, value): + self._autocommit = value + self.conn.select_one(_REQUEST_AUTOCOMMIT, (value,)) + def _new_conn(self): return SqliteMultithread( self.filename, @@ -513,6 +523,13 @@ def run(self): if req == _REQUEST_CLOSE: assert res_ref, ('--close-- without return queue', res_ref) break + elif req == _REQUEST_AUTOCOMMIT: + self.autocommit = arg[0] + if self.autocommit: + conn.isolation_level = None + else: + conn.isolation_level = "" + _put(res_ref, _RESPONSE_NO_MORE) elif req == _REQUEST_COMMIT: conn.commit() _put(res_ref, _RESPONSE_NO_MORE)