Transactions

Overview

Transactions are implemented for PostgreSQL and SQLite (2 and 3) only. The MySQL interface returns false on all three member functions.

The beginTransaction member function is special in that its return value, though boolean, does not mean "success/error". Instead, the return value means the following:

  • true: The transaction was started,
  • false: The transaction was not started.

The other two functions, commitTransaction and abortTransaction, follow the normal pattern of returning true on success, false on error.

Nested transactions are not supported. If a transaction is currently in progress, beginTransaction will return false.

C++ interface


#include <emdfdb.h>

class EMdFDB {
public:
  // Transactions
  virtual bool beginTransaction(void);
  virtual bool commitTransaction(void);
  virtual bool abortTransaction(void);
};

C++ example

The following shows how to use transactions. pDB is a pointer to the database object.


  bool bDoCommit;  // Should we commit later?
  bDoCommit = pDB->beginTransaction(); // Possibly begin transaction

  // Process...

  if (/* something went wrong */) {
    // Recover...
    if (bDoCommit) {
      if (!pDB->abortTransaction()) {
        // Abort failed
      }
    }
    // Exit from function
  }

  // More processing...

  // We're done!

  if (bDoCommit) {
    if (!pDB->commitTransaction()) {
      // Commit failed.
    }
  }


Previous:EMdF database
Up:EMdF database
Next:MQL execution environment