Sessions & Connections > Transaction Control
 
Transaction Control
Most RDBMSs can handle sequences of SQL statements as one block: all statements succeed or all fail. This typical behavior is called a transaction mechanism.
In some rare cases, the RDBMS is not capable of handling transactions, because this functionality either is not implemented or not enabled for the database, as with Informix.
A block is delimited by the initiation of the transaction and by its commitment or rollback.
The activation of the transaction mechanism can be checked by a call to one of the functions IldDbms::isTransactionEnabled or IldDbms::getInfo(IldTransactionCapable,...).
Rogue Wave DB Link offers a unified API that avoids RDBMS specificity. The member functions in the API take two optional arguments. However, for portability considerations, we urge you to always pass a value to each optional argument and to stick to the protocol that consists of sending the SQL statements by means of the IldRequest object that initiated the transaction. If you do not follow this rule when using MS SQL Server, you will encounter an unexpected behavior: the request that initiates the transaction is not the one that executes the SQL statements. This amounts to an empty transaction and results in the following:
*If the transaction is rolled back, the changes to the database will not be undone.
*It the transaction is committed, the changes are executed but they are not validated. Therefore, they are lost on disconnection.
With Rogue Wave DB Link, you can use member functions of the class IldDbms for any transaction-related command, whatever your target RDBMS, as shown in Table 3.5:
IldDbms Member Functions for Transaction-Related Commands
To
Use the IldDms member function
Comments
Initiate a transaction
Inoperative with Oracle and ODBC ports
Commit a transaction
 
Roll back a transaction
 
Switch the auto-commit mode on or off
Inoperative with Sybase and MS SQL Server.
For all these functions, the request argument (a pointer to an IldRequest object) is optional for most of the supported RDBMSs. However, this argument is mandatory for the Sybase and MS SQL Server database systems, which all require the IldRequest object to control the commands that execute the SQL statements enclosed in the transaction.
Some RDBMSs have implemented the “auto-commit mode” feature. When on, this mode commits each SQL statement when it is executed. Each database system has a unique notion of transaction control and, therefore, a unique interface to implement it.
With Sybase and MS SQL Server, the first argument is mandatory for all transaction-control functions. The second argument is used only for Sybase. Sybase TransactSQL allows you to name a transaction. For all other ports, both arguments are ignored. The following items relevant to transactions are described:
*Initiating a Transaction
*Committing a Transaction
*Rolling Back a Transaction
*Autocommit Mode
Initiating a Transaction
To initiate a transaction, call the member function IldDbms::startTransaction, as shown in the following example:
{
// A new transaction.
cout << "Initiating a transaction." << endl;
if (!dbms->startTransaction(request))
IldDisplayError("Begin transaction failed:", dbms);
}
This function takes two arguments, which are optional for most RDBMSs.
*If specified, the first argument must be a pointer to an IldRequest object, which is used to send the SQL statements that make up the body of the transaction.
*If specified, the second argument is a character string that is set as the transaction name.
With Informix, it is an error to start, commit, or roll back a transaction several times on a connection. Rogue Wave DB Link ensures that superfluous calls to these functions will not raise an error: they simply do nothing. However, you must be aware that the actual transaction still starts with the first call to IldDbms::startTransaction and ends with the first call to either IldDbms::commit or IldDbms::rollback.
Committing a Transaction
To commit a transaction, call the member function IldDbms::commit. This function takes two optional arguments, as shown in the following example.
{
cout << "Committing the transaction." << endl;
if (!dbms->commit(request))
IldDisplayError("Commit failed: ", dbms);
}
*If specified, the first optional argument must be a pointer to the IldRequest object used to send the SQL statements that make up the body of the transaction—namely, the same object that was used to initiate the transaction.
*If specified, the second optional argument is the same transaction name that was used to initiate the transaction.
Rolling Back a Transaction
To roll back all effects of the SQL statements executed since the transaction was initiated, call the member function IldDbms::rollback, as shown in the following example:
{
cout << "Rolling back the transaction." << endl;
if (!dbms->rollback(request))
IldDisplayError("Rollback failed:", dbms);
}
This member function takes two optional arguments, which must be the same as those used to initiate the transaction.
Autocommit Mode
To switch the autocommit mode on or off, use the member function IldDbms::autoCommitOn or IldDbms::autoCommitOff. Like other transaction-control functions, these functions take two optional arguments.
Note: The autocommit mode, while ensuring commitment of every successful SQL statement, exacts a very high price in terms of server work. You should avoid setting it on when it is not required by the application context.

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.