The C++ McoSqlSession Class

The McoSqlSession class is used to share a single database connection between multiple threads.

For an overview see page C++ Classes

This class must be used to share a single database connection between multiple threads. Multi-threaded applications can use their own SQL engine for each connected thread (i.e. create their own instance of McoSqlEngine). However, this is not convenient and not very efficient. In many cases the application does not start threads by itself, rather the thread pool is maintained by some external component and the user code is executed in the context of one of those threads. (A typical example is Apache servlets.)

McoSqlSession allows multiple threads to share a single McoMultithreadedSqlEngine instance. The constructor of this class uses function mco_db_connect() to establish a connection for a particular thread. The destructor of this class releases the connection by calling mco_db_disconnect().

McoSqlSession inherits all of the public methods of McoSqlEngine and its base class SqlEngine. The only additional specialized methods are its constructor and destructor as described in the table below.

McoSqlSession(McoSqlEngine* engine) Constructor: establishes a connection for the thread by calling mco_db_connect(); the argument engine is a share instance of McoMultithreadedSqlEngine
~McoSqlSession() Destructor: releases the connection by calling mco_db_disconnect()

Example

Typically, an McoSqlSession object is instantiated to manage transaction blocking in multi-threaded applications by calling it's Database member's beginTransaction() method. For example:

 
    McoMultithreadedSqlEngine engine;
    ...
     
    McoSqlSession session(&engine);
    Transaction* trans = session->database()->beginTransaction(Transaction::ReadWrite);
    session->executeStatement(trans, "update Member set balance=%i where id=%i",
                    fromBalance, idFrom);
    session->executeStatement(trans, "update Member set balance=%i where id=%i",
                    toBalance, idTo);
    trans->commit();
    trans->release();