You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know that there is little development but it might be useful for someone if using ZODB in 2022.
when trying to store changes to persistent objects inside a ZODB.DB.transaction with block.
they are not stored, and no error is raised.
while doing the same between transaction.begin() and transaction.commit() calls does work.
It seems to me that is the only way to currently use a with block is to change objects directly through conn.root(),
that means all persistent objects must know full path from root to themselves, which is impractical.
there is also another weird behavior. where after storing an object for the first time, and retrieving it returns the same object, while the 2nd call and up will return a different object.
this trips tests trying to check if something is stored successfully, as it only happens once.
the following code tries to store attributes in a 2 level persistent hierarchy (represents actual code)
importZODBimportZODB.FileStoragefrompersistent.mappingimportPersistentMappingimporttransactionstore=ZODB.FileStorage.FileStorage("temp1.db")
db=ZODB.DB(store)
defget_init(name, obj):
withdb.transaction(f"creating root[{name}]") asconn:
try:
returnconn.root()[name]
exceptKeyError:
conn.root()[name] =obj()
returnconn.root()[name]
classA:
def__init__(self):
self.cfg=PersistentMapping()
def__setitem__(self, key, value) ->None:
transaction.begin()
self.cfg[key+", inside block"] =valuetransaction.commit()
withdb.transaction():
self.cfg[key+", inside with"] =value#does not work#these should be equivalent, no?def__iter__(self):
returniter(self.cfg)
classManager:
def__init__(self):
self.a1=get_init("testing", PersistentMapping) # set up the db, should only happen oncedef__setitem__(self, name, obj) ->None:
"""Registers in persistent storage"""withdb.transaction(f"Adding testing:{name}") asconn:
ifnameinconn.root()["testing"]:
print(f"testing with same name {name} already exists in storage")
returnconn.root()["testing"][name] =objdef__getitem__(self, name: str):
returndb.open().root()["testing"][name]
dm=Manager()
initial=A() #only relevant for forst rundm['a']=initial#only relevant for forst runfromdb1=dm['a']
fromdb2=dm['a']
withdb.transaction() asconn:
fromdb1.cfg['updated from outer txn, directly'] =1#doed not workconn.root()['testing']['a'].cfg['updated from outer txn,through conn'] =1#this should be equivalent but only the second one works# transaction.begin()# transaction.commit()initial['new txn updated on initial'] =1fromdb1['new txn updated on retrieved 1']=1fromdb2['new txn updated on retrieved 2']=1# db.open().root()['testing']['a']['new txn updated on new db']=1print(f"initial obj - {initial.cfg}")
print(f"from db obj 1- {fromdb1.cfg}")
print(f"from db obj 2- {fromdb2.cfg}")
print(f"\nnew from db obj- {dm['a'].cfg}")
print(f"\nis the initial obj and the first obj from db the same: {initialisfromdb1}")
print(f"is the initial obj and the second obj from db the same: {initialisfromdb2}")
The expected result is for all writing methods to work similarly, or the docs to reflect those limitations.
Thanks!!
What version of Python and Zope/Addons I am using:
Python 3.9.2, ZODB3, Win1064bit
The text was updated successfully, but these errors were encountered:
I know that there is little development but it might be useful for someone if using ZODB in 2022.
when trying to store changes to persistent objects inside a ZODB.DB.transaction with block.
they are not stored, and no error is raised.
while doing the same between
transaction.begin()
andtransaction.commit()
calls does work.It seems to me that is the only way to currently use a
with
block is to change objects directly through conn.root(),that means all persistent objects must know full path from root to themselves, which is impractical.
there is also another weird behavior. where after storing an object for the first time, and retrieving it returns the same object, while the 2nd call and up will return a different object.
this trips tests trying to check if something is stored successfully, as it only happens once.
the following code tries to store attributes in a 2 level persistent hierarchy (represents actual code)
The expected result is for all writing methods to work similarly, or the docs to reflect those limitations.
Thanks!!
What version of Python and Zope/Addons I am using:
Python 3.9.2, ZODB3, Win1064bit
The text was updated successfully, but these errors were encountered: