[odb-users] Re: session required inside a function with a session

Boris Kolpackov boris at codesynthesis.com
Thu Jan 30 22:26:39 EST 2014

Hi Simón Emmanuel,

Simón Emmanuel Gutiérrez Brida <simon.gutierrez.brida at gmail.com> writes:

> At line 9 I get a session required exception.

Have you enabled session support for the User and Pattern classes? Citing
Chapter 11, "Session":

"Session support is optional and can be enabled or disabled on the per
 object basis using the db session pragma [...]
 We can also enable or disable session support for a group of objects
 at the namespace level [...]

 Finally, we can pass the --generate-session ODB compiler option to
 enable session support by default. With this option session support
 will be enabled for all the persistent classes except those for which
 it was explicitly disabled using the db session."

>    1. vector<User> Driver::retriveUsers() {
>    2.     session s;
>    3.     vector<User> users(0);
>    4.     transaction t (db->begin ());
>    5.
>    6.     user_result r (db->query<User> (true));
>    7.
>    8.     for (user_result::iterator i (r.begin ()); i != r.end (); ++i) {
>    9.         User* user = new User(*i);
>    10.         users.push_back(*user);

This is a very round-about and inefficient way of doing it. Instead,
you could simply do:


But, seeing that you have relationships, a much better version would
be to use shared_ptr:

           vector<std::tr1::shared_ptr<User> > users;

                 users.push_back(i.load ());

More information about the odb-users mailing list