【发布时间】:2011-08-03 00:45:54
【问题描述】:
我正在尝试使用 Django 的 ORM 通过 PostgreSQL 后端进行一些数据处理。给定一条主记录,在相关表中生成数百条记录,但如果发生任何错误,我想回滚所有记录创建。因此,我将整个过程包裹在手动事务中。
@transaction.commit_manually(main_record)
def save_data(data):
try:
create lots of records -> .save()
except Exception, e:
print 'Unexpected Error: %s' % e
transaction.rollback()
raise
else:
print 'Saving...'
try:
imgObj.processed = True
main_record.save()
transaction.commit()
print 'Saved!'
except Exception, e:
print 'Unexpected Error during commit: %s' % e
raise
但是,当我尝试 commit() 时,它会抛出异常“I/O operation on closed file”。谷歌搜索发现了一些关于这一点的提及,但关于 PG 的提及不多,也没有解决方案。这是什么意思?
编辑:我还注意到,当这种情况发生时,还有一个 postgres 进程标有我的数据库名称和状态“IDLE in transaction”。我应该结束这个过程吗?
【问题讨论】:
标签: python django postgresql django-models django-orm