Transaction Class
(CDP2SQL::Transaction)Class for executing database transactions. More...
Header: | #include <CDP2SQL/Transaction.h> |
Inherits: | HasDatabaseConnection |
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.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.