ZODB not able to commit

99封情书 提交于 2019-12-04 16:21:37

The ZODB persistence layer detects changes by hooking into the python __setattr__ hook, marking the persistent object as changed every time you set an attribute.

But if you use a primitive mutable object like a python dictionary, then there is no way for the persistence machinery to detect the changes, as there is no attribute being written. You have three options to work around this:

Use a persistent mapping

The persistent package includes a persistent mapping class, which is basically a python dictionary implementation that is persistent and detects changes directly by hooking into __setitem__ and other mapping hooks. The root object in your example is basically a persistent mapping.

To use, just replace all dictionaries with persistent mappings:

from persistent.mapping import PersistentMapping
root['layer'] = PersistentMapping()

Force a change detection by triggering the hook

You could just set the key again, or on a persistent object, set the attribute again to force the object to be changed:

root['layer'] = root['layer']

Flag the persistent object as changed

You can set the _p_changed flag on the nearest persistent object. Your root object is the only persistent object you have, everything else is python dictionaries, so you need to flag that as changed:

root._p_changed = 1

You are likely missing an

root['layer']._p_changed = 1

after modifiying the dict.

http://zodb.org/documentation/guide/prog-zodb.html?highlight=_p_changed#modifying-mutable-objects

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