with transaction() block doesn't work as expected
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
ZODB |
New
|
Undecided
|
Unassigned |
Bug Description
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 .commit() calls does work.
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)
```
import ZODB
import ZODB.FileStorage
from persistent.mapping import PersistentMapping
import transaction
store=ZODB.
db=ZODB.DB(store)
def get_init(name, obj):
with db.transaction(
try:
return conn.root()[name]
except KeyError:
return conn.root()[name]
class A:
def __init__(self):
self.cfg = PersistentMapping()
def __setitem__(self, key, value) -> None:
with db.transaction():
#these should be equivalent, no?
def __iter__(self):
return iter(self.cfg)
class Manager:
def __init__(self):
def __setitem__(self, name, obj) -> None:
with db.transaction(
if name in conn.root(
def __getitem__(self, name: str):
return db.open(
dm=Manager()
initial=A() #only relevant for forst run
dm['a']=initial #only relevant for forst run
fromdb1= dm['a']
fromdb2= dm['a']
with db.transaction() as conn:
fromdb1.
conn.
#this should be equivalent but only the second one works
# transaction.begin()
# transaction.
initial['new txn updated on initial'] = 1
fromdb1['new txn updated on retrieved 1']= 1
fromdb2['new txn updated on retrieved 2']= 1
# db.open(
print(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: {initial is fromdb1}")
print(f"is the initial obj and the second obj from db the same: {initial is fromdb2}")
```
The expected result is for storage methods to work.
or the docs to reflect those limitations.
Thanks!!