# Buggy code
def commit_now(self, vacuum=False, exiting=False):
    if self._should_commit and isInIOThread():
        try:
            self._logger.info("Start committing...")
            self.execute("COMMIT;")
        except:
            self._logger.exception("COMMIT FAILED")
            raise
        self._should_commit = False

        if vacuum:
            self._logger.info("Start vacuuming...")
            self.execute("VACUUM;")

        if not exiting:
            try:
                self._logger.info("Beginning another transaction...")
                self.execute("BEGIN;")
            except:
                self._logger.exception("Failed to execute BEGIN")
                raise
        else:
            self._logger.info("Exiting, not beginning another transaction")

    elif vacuum:
        self.execute("VACUUM;")


# Traceback (SQLError)
INFO    1542365691.13                   Session:800  (Session)  Tribler shutdown state notification:Shutting down SQLite Database...
INFO    1542365691.13             sqlitecachedb:237  (SQLiteCacheDB)  Start committing...
ERROR   1542365691.13             sqlitecachedb:294  (SQLiteCacheDB)  cachedb: ===MainThread===
SQL Type: <type 'unicode'>
-----
COMMIT;
-----
None
======
Traceback (most recent call last):
File "/home/sandip/tudelft/tribler/Tribler/Core/CacheDB/sqlitecachedb.py", line 283, in execute
return cur.execute(sql)
File "src/cursor.c", line 236, in resetcursor
SQLError: SQLError: cannot commit - no transaction is active
ERROR   1542365691.15             sqlitecachedb:240  (SQLiteCacheDB)  COMMIT FAILED
Traceback (most recent call last):
File "/home/sandip/tudelft/tribler/Tribler/Core/CacheDB/sqlitecachedb.py", line 238, in commit_now
self.execute(u"COMMIT;")
File "/home/sandip/tudelft/tribler/Tribler/Core/CacheDB/sqlitecachedb.py", line 296, in execute
raise msg
SQLError: SQLError: cannot commit - no transaction is active
ERROR   1542365691.15         shutdown_endpoint:47   (ShutdownEndpoint)  SQLError: cannot commit - no transaction is active

# Fixed code
def commit_now(self, vacuum=False, exiting=False):
    if self._should_commit and isInIOThread():
        try:
            self._logger.info("Start committing...")
            self.execute("COMMIT;")
        except:
            self._logger.exception("COMMIT FAILED")
            if exiting:
                # If we are exiting we don't propagate the error.
                # The reason for the exit may be the reason this exception occurred.
                self._logger.exception(
                    "Not propagating commit error, as we are exiting"
                )
                return
            else:
                raise
        self._should_commit = False

        if vacuum:
            self._logger.info("Start vacuuming...")
            self.execute("VACUUM;")

        if not exiting:
            try:
                self._logger.info("Beginning another transaction...")
                self.execute("BEGIN;")
            except:
                self._logger.exception("Failed to execute BEGIN")
                raise
        else:
            self._logger.info("Exiting, not beginning another transaction")

    elif vacuum:
        self.execute("VACUUM;")