[odb-users] Why do I get an odb::object_not_persistent error in this test program?

Daniel daniel.the.programmer at gmail.com
Wed Oct 30 09:31:20 EDT 2013

Hi Boris,

Thank you for your help - it all works perfectly now! I now use 
database_global->reload(parents[0]); at the start of the second 
transaction in server_thread(). (see attachment)

I wonder if this is the right solution? Do I need to use a mutex here, 
or does this solution always work?

Kind regards,


On 10/30/2013 01:49 PM, Boris Kolpackov wrote:
> Hi Daniel,
> Daniel <daniel.the.programmer at gmail.com> writes:
>> but the same error still occurs.
> Ok, I've figured this out. The first problem I mentioned in my email
> is real. It's just there is another one.
> In your server_thread() you have two transactions. In the first
> you load all the Parent objects. And in the second you add more
> children to them. The problem is between these two transactions
> your updater_thread() can (and does) modify (in the database) one
> of the loaded object by erasing its children. As a result, when
> you try to update the object in the second transaction (in
> server_thread), you attempt to store old (erased) child ids.
> If you need the two transactions, one way to fix this would be
> to store just the object ids in the first transaction and actually
> load the objects in the second.
>> "--fkeys-deferrable-mode immediate"
> If you want foreign key constraints in MySQL, then you should use
> 'not_deferrable' instead of 'immediate'. Once you do this, your
> code in updater_thread() should look something along these lines
> (i.e., you need to do things in a certain order):
> unsigned int id = (*parent->children.begin())->id;
> parent->children.erase(parent->children.begin());
> database_global->update (parent.get ());
> database_global->erase<Child>(id);
> Boris

-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 2120 bytes
Desc: not available
Url : http://codesynthesis.com/pipermail/odb-users/attachments/20131030/0ba682aa/main.cpp

More information about the odb-users mailing list