[odb-users] query results not being cached?

Boris Kolpackov boris at codesynthesis.com
Thu Jun 7 03:11:32 EDT 2012

Hi Craig,

Burton, Craig <crburton at tnsi.com> writes:

> Under many circumstances, our application expects to find exactly zero
> or one instance for many specific queries, but finding two or more would
> lead to an error scenario. Application logic is the following in many
> such cases:
>         if zero instances, create a new one
>         if one instance, update the one found in the db
>         if two or more, do nothing and return an error

If we can assume that the error scenario is quite rare and not performance-
sensitive, this can be handled optimally for the other two cases by doing
a "speculative" load (i.e., with the expectation that there is no error)
and then checking for the error condition:

result r = ...;

if (r.empty ())
  // Zero instances.
  result::iterator i (r.begin ());
  shared_ptr<MyPersistentClass> obj (i.load ());

  if (++i != r.end ())
    // More than one instance (error).

  // One instance.


More information about the odb-users mailing list