I was recently tasked with switching our database structure and was looking forward to using my new architecture to change the values in a few locations to get everything working, however I ran into a snag. To ensure all queries run safely and rollback with consistent logic, I put together the below Safe Txn Decorator. However, if system.db.runPrepUpdate is run in the decorated function (func) it ignores the database field and goes straight to using the project default. I removed the Decorator from the source function and it worked without issue.
def _Dec_SafeTxn():
def decorator(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
if not "txId" in kwargs or not kwargs["txId"]:
txn = system.db.beginTransaction(self.database.name)
isTxId = False
else:
txn = kwargs["txId"]
isTxId = True
del kwargs["txId"]
try:
out = func(self, *args, txId = txn, **kwargs)
if isTxId:
if self.testMode:
system.db.rollbackTransaction(txn)
else:
system.db.commitTransaction(txn)
except java.lang.Exception as e:
if not isTxId:
system.db.rollbackTransaction(txn)
if e.getCause():
message = e.getCause().getMessage()
else:
message = str(e)
Logger(loggerName = 'Safe Txn Logger').error(message)
raise
except Exception as e:
if not isTxId:
system.db.rollbackTransaction(txn)
LoggerException(loggerName = 'Safe Txn Logger').error(e)
raise
finally:
if not isTxId:
system.db.closeTransaction(txn)
return out
return wrapper
return decorator
This was inconvenient during testing and I can get by with changing the project Database, but want to know if this is a bug or I am doing something wrong?