[ZODB-Dev] zodb does not save transaction

tsmiller tsmiller at gnixterhouse.com
Thu May 29 18:33:02 EDT 2008



Laurence Rowe wrote:
> 
> tsmiller wrote:
> 
>>>> I have a bookstore that uses the ZODB as its storage.  It uses qooxdoo
>>>> as
>>>> the client and CherryPy for the server.  The server has a
>>>> 'saveBookById'
>>>> routine that works 'most' of the time.  However, sometimes the
>>>> transaction.commit() does NOT commit the changes and when I restart my
>>>> server the changes are lost.
> 
>>> This sounds like you are using mutable data types (like lists or dicts)
>>> in
>>> the
>>> non-persistence aware variants.
> 
>> Christian, thanks for the reply.
>> When I save a book I save a dictionary where all of the keys are strings
>> and
>> all of the values are strings. But what you say makes sense.  I keep
>> thinking that it must have something to do with the data itself.  I will
>> check very carefully to make sure that I am not saving anything but
>> strings
>> in the book record.  Thanks.  Tom
> 
> The problem is not saving things that are not strings, but modifying a 
> non persistent object without notifying the parent persistent object 
> that a change has happened and it needs to be saved.
> e.g.
> 
> you have a persistent object (inherits from persistent.Persistent) pobj
> 
>  >>> pobj.dict = {}
>  >>> transaction.commit()
>  >>> pobj.dict['foo'] = 'bar'
>  >>> transaction.commit()
>  >>> print pobj.dict
> {'foo': 'bar'}
> 
> #restart your python process
>  >>> print pobj.dict
> {}
> 
> Instead you must either tell zodb the object has changed:
> 
>  >>> pobj.dict = {}
>  >>> transaction.commit()
>  >>> pobj.dict['foo'] = 'bar'
>  >>> pbj._p_changed = True # alternatively: pobj.dict = pobj.dict
>  >>> transaction.commit()
>  >>> print pobj.dict
> {'foo': 'bar'}
> 
> #restart your python process
>  >>> print pobj.dict
> {'foo': 'bar'}
> 
> Or use a persistence aware replacement.
> 
>  >>> from persistent.mapping import PersistentMapping
>  >>> pobj.dict = PersistentMapping()
>  >>> transaction.commit()
>  >>> pobj.dict['foo'] = 'bar'
>  >>> transaction.commit()
>  >>> print pobj.dict
> {'foo': 'bar'}
> 
> #restart your python process
>  >>> print pobj.dict
> {'foo': 'bar'}
> 
> The same principles apply to other mutable non-peristent objects, such 
> as lists.
> 
> Laurence
> 
> _______________________________________________
> For more information about ZODB, see the ZODB Wiki:
> http://www.zope.org/Wikis/ZODB/
> 
> ZODB-Dev mailing list  -  ZODB-Dev at zope.org
> http://mail.zope.org/mailman/listinfo/zodb-dev
> 
> 

Laurence,
Thank you and all of you.  I understand the principle and right off the bat
I do not see why I am breaking it, but I will look hard and long at the
roblem.  Thank you for giving me a starting point.
Tom


-- 
View this message in context: http://www.nabble.com/zodb-does-not-save-transaction-tp17526533p17547540.html
Sent from the Zope - ZODB-Dev mailing list archive at Nabble.com.



More information about the ZODB-Dev mailing list