ZODB 无法提交
我是第一次使用 ZODB。只是尝试使用 FileStorage 提交数据。但是当我第二次执行相同的脚本时,我无法提交新对象。这是我的脚本
from ZODB import FileStorage,DB
import transaction
storage = FileStorage.FileStorage('/tmp/test.fs')
db = DB(storage)
conn = db.open()
root = conn.root()
#root['layer']={}
root['layer']['2b']={"id":'2b','name':'some name'}
transaction.commit()
conn.close()
db.close()
storage.close()
,当我再次重复代码时,只需更改 id root['layer']['2c']
并从 python 中出来,第二次对象不会被提交。我只有第一个对象。可能是什么原因。
I am using ZODB first time. Just trying to commit the data with FileStorage. But when I do the same script second time I am not able to commit the new object. Here is my script
from ZODB import FileStorage,DB
import transaction
storage = FileStorage.FileStorage('/tmp/test.fs')
db = DB(storage)
conn = db.open()
root = conn.root()
#root['layer']={}
root['layer']['2b']={"id":'2b','name':'some name'}
transaction.commit()
conn.close()
db.close()
storage.close()
when I repeat the code once again, with just changing the id root['layer']['2c']
and come out of the python, the second time object is not getting committed. I only have first object only. What could be the reason.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ZODB 持久层通过挂钩到 python
__setattr__
挂钩来检测更改,并在每次设置属性时将持久对象标记为已更改。但是,如果您使用像Python字典这样的原始可变对象,那么持久性机制就无法检测到更改,因为没有写入任何属性。您可以通过三个选项来解决此问题:
使用持久性映射
持久性包包含一个持久性映射类,它基本上是一个持久性的 Python 字典实现,并通过挂接到 __setitem__ 和其他映射来直接检测更改钩子。您示例中的
root
对象基本上是一个持久映射。使用时,只需用持久映射替换所有字典:
通过触发挂钩强制进行更改检测
您可以再次设置密钥,或者在持久对象上再次设置属性以强制更改对象:
将持久对象标记为已更改
您可以在最近的持久对象上设置
_p_changed
标志。你的根对象是你拥有的唯一持久对象,其他一切都是Python字典,所以你需要将其标记为已更改: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. Theroot
object in your example is basically a persistent mapping.To use, just replace all dictionaries with persistent mappings:
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:
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:您可能错过了
修改字典后的内容。
http://zodb.org/documentation/guide /prog-zodb.html?highlight=_p_changed#modifying-mutable-objects
You are likely missing an
after modifiying the dict.
http://zodb.org/documentation/guide/prog-zodb.html?highlight=_p_changed#modifying-mutable-objects