beginTransaction

Start a database transaction creating a Transaction instance.

For an overview see page McoSqlEngine

Prototypes

    Transaction* SqlEngine::beginTransaction(Transaction::Mode mode,
                            int priority = 0,
                            Transaction::IsolationLevel level =
                            Transaction::DefaultIsolationLevel)

Arguments

mode
A Transaction::Mode: Exclusive, ReadOnly, ReadWrite or Update.
priority
An integer value specifying the transaction priority. (See page Transaction Priorities and Scheduling for further details.)
level
A Transaction::IsolationLevel: DefaultIsolationLevel, ReadCommitted, RepeatableRead or Serializable. (See page Transaction Isolation Levels for further details.)

Description

This method starts a database transaction creating a Transaction instance. When completed, the Transaction method commit() or rollback() must be called to close the transaction, and release() to release system resources associated with the transaction. In multi-threaded applications, if it is necessary to share a single database connection between multiple threads, the McoSqlSession class must be used and its Database member's beginTransaction() method must be called.

Returns

This method returns a Transaction or throws a RuntimeException in the case of an error.

Example

    const char * db_name = "SqlDb";

    int main(int argc, char* argv[])
    {
        McoSqlEngine engine;
        ...
        engine.open(db_name,  // database name
                SqlDb_get_dictionary(),  // database dictionary
                DATABASE_SIZE,  // database size
                MEMORY_PAGE_SIZE,  // page size
                MAP_ADDRESS); // mapping address for shared memory mode
        ...
        Transaction* trans = engine.beginTransaction(Transaction::ReadWrite);

        engine.executeStatement(trans, "update Member set balance=%i where id=%i",

                        fromBalance, idFrom);

        engine.executeStatement(trans, "update Member set balance=%i where id=%i",

                        toBalance, idTo);

        trans->commit();

        trans->release();

        ...
    }