dbBeginTransaction()
Database function for starting a transaction.
Synopsis
int dbBeginTransaction(dbConnection connection);
Parameters
Parameter | Meaning |
---|---|
connection | Connection reference provided by dbOpenConnection() |
Return Value
The return value is an error code; 0 indicates the operation has been performed successfully. For other values dbGetError() can be used to obtain detailed error information.
Errors
An error is returned if an invalid connection is passed, or if the data source does not support transactions.
Description
In order to be able to group changes in a data source into transactions, the application must call the function dbBeginTransaction(). The transaction is terminated with dbCommitTransaction() (changes implemented) or with dbRollbackTransaction() (changes discarded). All changes made by the application to the data source between these two instructions (whether using dbExecuteCommand() or via a record set), are considered as one total manipulation.
Nesting of transactions is not allowed for some types of data source. Also, some data sources start transactions implicitly (when a connection is made and after dbCommitTransaction() or dbRollbackTransaction()). Simple data sources may also have no transaction support.
Parallel transactions are possible with several dbConnections open at the same time.
Example
main()
{
int rc;
dbConnection conn;
dbCommand cmd;
string sql;
rc = dbOpenConnection ("DSN=Pubs;UID=sa;PWD=pwd;",
conn); // Opens connection
if (!rc)
{
rc = dbBeginTransaction (conn); //Start of transaction
if (!rc)
{
sql = "UPDATE EMP SET SALARY = SALARY * 1.1"
// SQL query of a salary list from the DB
rc = dbStartCommand (conn, sql, cmd);
// Preparing the command
if (!rc)
{
rc = dbExecuteCommand (cmd); // Execute
dbFinishCommand (cmd); // End of the command
if (!rc)
{
sql = "UPDATE DEPT SET UPDATED = 'Y'"
rc = dbStartCommand (conn, sql, cmd);
if (!rc)
{
rc = dbExecuteCommand (cmd);
dbFinishCommand (cmd);
}
}
}
if (!rc)
rc = DbCommitTransaction (conn);
// Changes to DB are implemented
else
rc = dbRollbackTransaction (conn);
// Changes to DB are discarded
}
dbCloseConnection(conn); // Connection closed
}
}
Assignment
ADO and Qt, Database functions
Availability
CTRL