[ZODB-Dev] How to cause commit with non-PersistentDict?

Marius Gedminas marius at gedmin.as
Tue Jun 19 21:24:47 UTC 2012


On Tue, Jun 19, 2012 at 04:22:43PM -0400, Claudiu Saftoiu wrote:
> Hello all,
> 
> Say I have a:
> 
>     class Foo(Persistent):
>         def __init__(self, bar):
>             self.my_dict = PersistentDict({'keyis': bar})
>         def update_it(self, bar):
>             self.my_dict['keyis'] = bar
> 
> If I want to use a `dict`, instead (it seems it might be faster for my
> larger example),

I doubt that.

(OTOH if you have a large number of keys, consider using OOBTree instead
of PersistentDict.)

> how would I cause a change to the dict to
> be committed? Is there any way other than this?
> 
>     class Foo(Persistent):
>         def __init__(self, bar):
>             self.my_dict = {'keyis': bar}
>         def update_it(self, bar):
>             self.my_dict['keyis'] = bar
>             self.my_dict = dict(self.my_dict)

Yes: either

          def update_it(self, bar):
              self.my_dict['keyis'] = bar
              self.my_dict = self.my_dict

or

          def update_it(self, bar):
              self.my_dict['keyis'] = bar
              self._p_changed = True

I recommend against this pattern in general: it's too easy to forget the
_p_changed bit and end up with bugs that are hard to notice.  Because
ZODB caches object instances, nonpersistent subobject modifications live
on in memory until those objects get flushed (or Zope gets restarted),
so you only notice you've a bug in your code when changes start
mysteriously disappearing a few days after they're made.

Marius Gedminas
-- 
The Feynman Problem Solving Algorithm:
1) Write down the problem.
2) Think very hard.
3) Write down the solution.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://mail.zope.org/pipermail/zodb-dev/attachments/20120620/52acc97e/attachment.sig>


More information about the ZODB-Dev mailing list