ZODB 无法提交

发布于 2024-11-02 08:48:43 字数 492 浏览 0 评论 0原文

我是第一次使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-11-09 08:48:43

ZODB 持久层通过挂钩到 python __setattr__ 挂钩来检测更改,并在每次设置属性时将持久对象标记为已更改。

但是,如果您使用像Python字典这样的原始可变对象,那么持久性机制就无法检测到更改,因为没有写入任何属性。您可以通过三个选项来解决此问题:

使用持久性映射

持久性包包含一个持久性映射类,它基本上是一个持久性的 Python 字典实现,并通过挂接到 __setitem__ 和其他映射来直接检测更改钩子。您示例中的 root 对象基本上是一个持久映射。

使用时,只需用持久映射替换所有字典:

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

通过触发挂钩强制进行更改检测

您可以再次设置密钥,或者在持久对象上再次设置属性以强制更改对象:

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

将持久对象标记为已更改

您可以在最近的持久对象上设置_p_changed 标志。你的根对象是你拥有的唯一持久对象,其他一切都是Python字典,所以你需要将其标记为已更改:

root._p_changed = 1

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
绝不服输 2024-11-09 08:48:43

您可能错过了

root['layer']._p_changed = 1

修改字典后的内容。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文