[odb-users] How to operate shared mysql databases and tables through ODB?

Boris Kolpackov boris at codesynthesis.com
Fri Aug 6 07:02:51 EDT 2021

karnlai(赖正敏) <karnlai at tencent.com> writes:

> As the data volume is growing rapidly, we need to use shared database
> and tables in our project to ensure performance and reliability.
> Suppose we have db_00, db_01,…, db10, and each db has tb_00, …., tb…10,
> where the structure of each db and each table is same. They only differ
> in name.
> According to the doc of ODB, each entity is mapped to a table, but it
> is inefficient to create 10 different classes with same fields for the
> 10 tables which only differ in name, right?

Yes, an ODB persistent class maps to a specific database table name
(there are good reasons for this, such as the use of prepared statements).
So you will need to use MySQL-level mechanisms to establish the "context"
in which this name is resolved. One such mechanism is the USE statement[1]
which allows you to designate a specific database as the default:


I don't believe there is a similar mechanism for tables so if you need
this, duplicating the class (perhaps with an abstract base class and a
template implementation) is your best option.

More information about the odb-users mailing list