Stop transaction if Cache.clean failed during start

Otherwise the already created connection is never put back to the pool.

issue7532
review49481002
This commit is contained in:
Cédric Krier committed 2018-07-02 23:18:40 +02:00
1 parent 6edad5dba6
commit 686123bd73
2 files changed
+18 -4

No files matched your search

+7
View File
@@ -51,6 +51,13 @@ commited.
transaction will be committed when exiting the `with` statement without
exception. The other cases will be rollbacked.
.. method:: Transaction.stop([commit])
Stop the transaction. If commit is True, the transaction will be committed
otherwise it will be rollbacked.
The `context manager`_ returned by :meth:`Transaction.start` should be used
instead of calling this method.
.. method:: Transaction.set_context(context, \**kwargs)
Update the transaction context and return a `context manager`_. The context
+11 -4
View File
@@ -87,11 +87,11 @@ class Transaction(object):
else:
database = Database(database_name).connect()
Flavor.set(Database.flavor)
self.connection = database.get_connection(readonly=readonly,
autocommit=autocommit)
self.user = user
self.database = database
self.readonly = readonly
self.connection = database.get_connection(readonly=readonly,
autocommit=autocommit)
self.close = close
self.context = context or {}
self.create_records = {}
@@ -103,19 +103,26 @@ class Transaction(object):
self._nocache = _nocache
if not _nocache:
from trytond.cache import Cache
Cache.clean(database.name)
try:
Cache.clean(database.name)
except BaseException:
self.stop(False)
raise
return self
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.stop(type is None)
def stop(self, commit=False):
transactions = self._local.transactions
try:
if transactions.count(self) == 1:
try:
try:
if type is None and not self.readonly:
if commit and not self.readonly:
self.commit()
else:
self.rollback()