A .NET transaction is created by calling the BeginTransaction( ) method on the Connection object. All subsequent operations associated with the transaction, such as committing or aborting the transaction, are performed using the Transaction object.
Transactions should be used only when required. Using transactions imposes a performance penalty due to the system overhead in managing the transaction. Transactions can also block work of other users in the system, which causes performance problems. For that reason, if transactions are required, the isolation level of the transactions should be carefully considered.
Because the Transaction is a connected class, each .NET data provider implements its own Transaction class, with a similar interface and function as other Transaction classes but in its own unique namespace. The Transaction class namespaces for several ADO.NET data providers are listed in Table 31-1.
Class |
Data source |
---|---|
System.Data.SqlClient.SqlTransaction |
SQL Server |
System.Data.OleDb.OleDbTransaction |
OLE DB provider |
Microsoft.Data.Odbc.OdbcTransaction |
ODBC driver |
System.Data.OracleClient.OracleTransaction |
Oracle |
The commonly used public properties of the Transaction class are listed in Table 31-2.
Property |
Description |
---|---|
Connection |
Gets the connection associated with the transaction. |
IsolationLevel |
Gets the isolation level for the transaction. |
The commonly used public methods of the Transaction class are listed in Table 31-3.
Method |
Description |
---|---|
Begin( ) |
Starts a nested transaction (OLE DB only). |
Commit( ) |
Commits the transaction. |
Rollback( ) |
Rolls back a transaction from a pending state. |
Save( ) |
Creates a savepoint in the transaction that can be used to roll back a portion of the transaction (SQL Server only). |
The Transaction class inherits from MarshalByRefObject and implements the IDbTransaction and IDisposable interfaces. Public static members of this class are safe for multithreaded operations; instance members aren't guaranteed thread-safe.