• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
CDP Studio logo

CDP Studio

The no-code and full-code software development tool for distributed control systems and HMI

  • Doc
  • Why CDP
    • Software developers
    • Automation engineers
    • Managers
  • Products
    • Automation Designer
    • HMI Designer
    • Maritime HMIs
  • Services
  • Use cases
  • Pricing
  • Try CDP

CDP Studio Documentation

  • Framework - CDP2SQL
  • Transaction
  • 5.0.0

Transaction Class

(CDP2SQL::Transaction)

Class for executing database transactions. More...

Header: #include <Transaction>
Inherits: HasDatabaseConnection
  • List of all members, including inherited members

Public Functions

Transaction(bool autoCommit = false)
Transaction(Database &db, bool autoBegin = false, bool autoCommit = false)
Transaction(const Transaction &other) = delete
~Transaction() override
void Begin()
void Commit()
void Rollback()
  • 2 public functions inherited from CDP2SQL::HasDatabaseConnection

Detailed Description

Class for executing database transactions.

Especially useful in case of exceptions, as it will by default rollback changes when the object goes out of scope. (Thus no need for a lot of nested try-catch loops.)

Note that nested transactions are not supported, due to varying support in database implementations.

Example usage:

#include <CDP2SQL/CDP2SQL.h>
#include <CppSQLite3/SQLite3Factory.h>

...

using namespace CDP2SQL;
try {
  Database db(SQLite3Factory().Create(), "mysqlite3databasefile.db");
  Transaction t(db);
  t.Begin();   // Starts the transaction.
  Statement st(db, "insert into employee values (?, ?, ?);");
  st.Bind(1, 123); st.Bind(2, "John Doe"); st.Bind(3, 64000.99); st.Execute(); st.Reset();
  st.Bind(1, 456); st.Bind(2, "Jane Doe"); st.Bind(3, 63999.99); st.Execute(); st.Reset();
  t.Commit();   // Commit the changes to the database.
} catch (const SQLException& e) {   // Transaction is automatically rolled back in case of exception as it goes out of scope.
  printf("Error occurred: %s\n", e.Message());   // Neither John nor Jane Doe was added to the database if failure.
}

Member Function Documentation

Transaction::Transaction(bool autoCommit = false)

Default constructor. Only use this if you have to delay connecting it to a Database object.

If autoCommit is true, the transaction object will commit when it goes out of scope, instead of rollback, which is the default.

Transaction::Transaction(Database &db, bool autoBegin = false, bool autoCommit = false)

Creates transaction object. The database db must be connected.

If autoBegin is true, the transaction will be started immediately.

If autoCommit is true, the transaction object will commit when it goes out of scope, instead of rollback, which is the default.

[delete] Transaction::Transaction(const Transaction &other)

Copy constructor.

Transaction::~Transaction()

Destructor for the transaction.

Will by default automatically rollback any uncommited changes. This behaviour can be changed in the constructor.

void Transaction::Begin()

Starts a transaction on the database. Any changes done on the database will now not be committed until Commit() is called.

void Transaction::Commit()

Commits all pending changes to the database.

void Transaction::Rollback()

Rollback and undo any pending changes to the database.

This will by default happen automatically when the Transaction object goes out of scope.

The content of this document is confidential information not to be published without the consent of CDP Technologies AS.

CDP Technologies AS, www.cdpstudio.com

Get started with CDP Studio today

Let us help you take your great ideas and turn them into the products your customer will love.

Try CDP Studio for free
Why CDP Studio?

CDP Technologies AS
Hundsværgata 8,
P.O. Box 144
6001 Ålesund, Norway

Tel: +47 990 80 900
E-mail: info@cdptech.com

Company

About CDP

Contact us

Services

Partners

Blog

Developers

Get started

User manuals

Support

Document download

Release notes

My account

Follow CDP

  • LinkedIn
  • YouTube
  • GitHub

© Copyright 2025 CDP Technologies. Privacy and cookie policy.

Return to top