[odb-users] ODB custom session notifications on transaction rollback

Boris Kolpackov boris at codesynthesis.com
Wed Nov 25 06:40:24 EST 2015


Hi Konstantin,

Konstantin Tarovik <konstantin.tarovik at ab-soft.net> writes:

>         odb::transaction t(_db->begin());
> 
>         _db->persist(c);
> 
>         t.commit();
> 
> During persist call (even before commit is called) I get the
> notification inside custom session telling that the element has been
> stored to the DB cache (actually I receive two callbacks in a row
> _cache_persist and _cache_insert). This is not perfect behavior IMO
> but it's ok. The real issue arises when I rollback the transaction:
> 
>         odb::transaction t(_db->begin());
> 
>         _db->persist(c);
> 
>         t.rollback();
> 
> I still receive the notification inside persist call telling that
> the element has been added. But when I rollback the transaction
> _cache_erase notification is not being called. Taking into account
> that I use notifications to make data consistent between the DB and
> UI this behavior causes data inconsistency.

The custom session interface is intentionally low-level in order to
allow various session/cache semantics. For example, some session
implementation may only be intended to live for as long as the
transaction. For such a session any kind of cross-transaction
state tracking would be unnecessary.

But if you do want the semantics that you have described, then you
will need to implement it on top of this low-level session interface
with the help of transaction callbacks (Section 15.1, "Transaction
Callbacks"). The idea is to first insert the objects "tentatively"
(e.g., by setting a flag). Then, in the transaction callback,
depending on whether it is committed or rolled back, you either
"finalize" such objects (e.g., by clearing the flag) or remove them
from the session.

In fact, the change-tracking session implementation in the 
common/session/custom test in the odb-tests package does something
very similar: it clears the change flag on an object entry only if
and when the transaction is committed.

Boris



More information about the odb-users mailing list