[odb-users] Creation of ODB cache

Davide Anastasia Davide.Anastasia at qualitycapital.com
Thu May 31 10:25:17 EDT 2012


Hi Boris,
I'm back! :)

Suppose I try to read from the database an object using load<OBJ>(KEY)
and the KEY does not exist, I get an exception of type
object_not_persistent. That's fair. However, I would love to have a
different exception for each persistent type: is it that possible
somehow? It would be great to be able to do something like:

Try
{
  Shared_ptr<obj1> myObj1 = db.load<obj1>(keyObj1);
  Shared_ptr<obj2> myObj2 = db.load<obj2>(keyObj2);
}
Catch (const obj1_not_persistent& ex)
{
  ...
}
Catch (const obj2_not_persistent& ex)
{
   ...
}

Is it that possible? Or should I try { } catch () {} for each db.load()?

Thanks,
Davide

-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com] 
Sent: 30 May 2012 15:01
To: Davide Anastasia
Cc: odb-users at codesynthesis.com
Subject: Re: [odb-users] Creation of ODB cache

Hi Davide,

Davide Anastasia <Davide.Anastasia at qualitycapital.com> writes:

> 1. can a session object be a member of a class? 

The session that is currently provided by ODB is per-thread (a pointer
to the current session is stored in the TLS). It is also not thread-
aware in the sense that a single session cannot be used safely from
multiple threads concurrently.

However, one can imagine that a shared, thread-aware implementation
could be useful for some applications so we could add something like
this in the future.

So, to answer your question, yes, you can make a session object a member
of a class though it will "attach" itself to the thread where the class'
constructor is executed, which may or may not be what you want (you can
"detach" it from the current and attach it to another thread using the
provided "current" API).

> I'm saying that because my class essentially runs into an std::thread 
> and waits for "updates" to store, starting a transaction every time:
> 
> void run()
> {
> 	while (1)
> 	{
> 		// wait
> 		Transaction t( ...) ;
> 
> 		// load, persist, ?
> 
> 		t.commit();
> 	}
> }

In this case it seems more straightforward to just do:

void run()
{
  session s;

  while (1)
  {
    ..
  }
}

Unless you plan to restart the thread and want to keep the session's
contents.


> 2. how many session object can my application have? Can it make sense 
> to have a session at the beginning of the application as "master"
session?

I think the above explanation should make this clear now. Currently, you
can have one active session per thread. 


> 3. If I do a conditional search, is the retrieved object stored in the

> session?

You mean if you execute a query? In that case, yes, if you actual
instantiate the object (i.e., dereference the iterator), then yes, the
object will be cached. 


> 4. Does session apply to Views?

No, views are not stored in a session (they have no object ids).

 
> To many questions, I know :)

No problem. It is always good to understand how people are using (or
trying to use) ODB.

Boris



More information about the odb-users mailing list