Robotframework-Database-Library
Robotframework-Database-Library copied to clipboard
I am wondering why do we need a rollback() method after commit() method in execute_sql_string function?
In def execute_sql_string(self, sqlString, sansTran=False)
cur = None
try:
cur = self._dbconnection.cursor()
logger.info('Executing : Execute SQL String | %s ' % sqlString)
self.__execute_sql(cur, sqlString)
if not sansTran:
self._dbconnection.commit()
finally:
if cur:
if not sansTran:
self._dbconnection.rollback()
The question is a bit old, but I'll answer it anyway.
If the execution of the sql statement executed properly (and there is no transaction), then the commit statement shall be executed. The database contents shall have been changed (permanently) by this. Then the finally is executed and a rollback of everything between the last commit and now is rolled back; nothing is rolled back; no harm, no foul.
But if the sql statement did not properly execute, the commit line shall not be reached but skipped. There could be any sort of exception to catch, but that is difficult to predict so no catch clause exists in the above code. The finally phase shall always be reached and that is enough to inform the database to roll back everything from the statement and recover from whatever exception there might have been.