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 callingmco_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 below.
McoSqlSession(McoSqlEngine* engine)Establishes a connection for the thread by callingmco_db_connect(); the argumentengineis a shared instance of McoMultithreadedSqlEngine.~McoSqlSession()Releases the connection by callingmco_db_disconnect().Example
Typically, an McoSqlSession object is instantiated to manage transaction blocking in multi-threaded applications by calling its 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();