Peewee says “cannot commit - no transaction is active”

痴心易碎 提交于 2019-12-12 02:45:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!