问题
My CherryPy app does some cleaning every hour with the following code:
def every_hour():
two_hours_ago = time.time() - 2 * 60 * 60
DbChoice.delete().where(DbChoice.time_stamp < two_hours_ago).execute()
monitor_every_hour = Monitor(cherrypy.engine, every_hour, frequency=60 * 60)
monitor_every_hour.start()
Sometimes it crashes with he following message:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\peewee.py", line 2364, in execute_sql
self.commit()
File "C:\Python34\lib\site-packages\peewee.py", line 2371, in commit
self.get_conn().commit()
sqlite3.OperationalError: cannot commit - no transaction is active
This thread and others talk about how to fix the problem when working directly with sqlite, but I am using Peewee and I don't know if I am doing something wrong with Peewee or it is a bug and I need to work around it.
I start the connection with:
db = peewee.SqliteDatabase(path_name + '/doc.db', check_same_thread=False)
回答1:
Looks like the query is automatically committed by default. So Try setting autocommit to False.
db = peewee.SqliteDatabase(path_name + '/doc.db', check_same_thread=False)
db.set_autocommit(False)
http://peewee.readthedocs.org/en/2.0.2/peewee/cookbook.html#changing-autocommit-behavior
Hope this helps!
来源:https://stackoverflow.com/questions/25533166/peewee-says-cannot-commit-no-transaction-is-active