[odb-users] ODB in a multi-threaded environment

Boris Kolpackov boris at codesynthesis.com
Thu Jun 23 07:42:17 EDT 2011


Hi Nicolas,

Nicolas ALBEZA <n.albeza at gmail.com> writes:

> I was wondering how to use odb::database in a multi-threaded environment :
>  - Does i need to instantiate a database for each thread ?
>  - If this is the case, how caching is handled ?

In a multi-threaded application you would normally have a group of threads
use the same odb::database instance (it is thread-safe and you can call its
functions without any extra locking). In this setup it also makes sense
to use the connection_pool factory (it is the default for MySQL and SQLite).
You can also configure the pool with the maximum number of connections it
should maintain, etc. For more information on the available connection
factories as well as their semantics and parameters, see the "Connection
Factory" sections in PART II, "Database Systems" of the manual.


>  - Is odb::database a light object that i can instantiate and destroy
> without much overhead ?

This depends on the underlying connection factory used. If, for example,
you use the new_connection_factory, then yes, the database instance is
quite light and can be created/destroyed often. On the other hand, if
you are using the pool factory, then the database instance can be quite
"heavy", especially if it is configured to start up with a large number
of connections (the min_connections parameter).

Boris



More information about the odb-users mailing list