Project

General

Profile

Correct way to use Wt::Dbo to connect to multiple databases

Added by Anonymous over 9 years ago

So the application I'm working on is a bridge between multiple mysql databases, and maintains its own local mysql database for relational data between the others.

I want to decouple the database functions from the UI code in order to maximize reuse for command line, so I have interface classes. But I also don't want to the UI to be handing around references to the database sessions (I'd prefer the UI code to not even know there is a database), so I'm thinking of using a singleton I'm calling DBSessionPools that holds the references to the individual SqlConnectionPool instances. Then my interfaces just need to ask the singleton for the appropriate reference(s) instead of having it passed.

This was working out well for the local database, but as soon as I added my second SqlConnectionPool to the singleton and tried to get the reference, I segfault the server.

The tutorials all deal with how to connect and interact with a single database.. Any recommendations on methodologies for multi-database connectivity?


Replies (1)

RE: Correct way to use Wt::Dbo to connect to multiple databases - Added by Eivind Midtgård over 9 years ago

I do this:

Wt::Dbo::SqlConnectionPool* createConnectionPool(const std::string& sqliteDatabasePath, bool showQueries, int nbrConnections)
{
    Wt::Dbo::backend::Sqlite3* connection = new Wt::Dbo::backend::Sqlite3(sqliteDatabasePath);

    connection->setProperty("show-queries", (showQueries ? "true" : "false"));
    connection->setDateTimeStorage(Wt::Dbo::SqlDateTime, Wt::Dbo::backend::Sqlite3::PseudoISO8601AsText);

    return new Wt::Dbo::FixedSqlConnectionPool(connection, nbrConnections);
}

auto database1   = new Wt::Dbo::Session;
auto connection1 = createConnectionPool("first/path/db1.db", false, 1);
database1->setConnectionPool(*connection1);

auto database2   = new Wt::Dbo::Session;
auto connection2 = createConnectionPool("second/path/db2.db", false, 1);
database2->setConnectionPool(*connection2);

auto data1 = database1.find<Something>().where("name = ?").bind("Tom").resultList();
auto data2 = database2.find<Something>().where("name = ?").bind("Moore").resultList();

I don't use MySQL, but believe the method to be the same.

Regards,

Eivind

    (1-1/1)