Before System. Transaction was released, you could create transactions directly with AOO.NET, or you could do transactions with the help of components, attributes, and the COM+ runtime, which is covered in the namespace System. EnterpriseServices. To show you how the new transaction model compares to the traditional ways of working with transactions, we present a short look at how AOO.NET transactions and transactions with Enterprise Services are done
Let’s start with.traditional AOO.NET transactions. H you don’t create transactions manually, there is a single transaction with every SQL statement. H multiple statements need to participate with the same transaction, however, you must create a transaction manually to achieve this.
The following code segment shows how to work with AOO.NET transactions. The SqlConnection class defines the method BeginTransaction ( ), which returns an object of type Sql Transaction. This transaction object must then be associated with every command that participates with the transaction. To associate a command with a transaction, set the Transaction property of the SqlCommand class to the Sql Transaction -instance. For the transaction to be successful, you must invoke the Commit () method of the Sql Transaction object. H there is an error, you have to invoke the Rollback () method, and every change is undone. You can check for an error with the help of a try / ca tch and do the rollback inside the catch.
If you have multiple commands that should run in the same transaction, every command must be associated with the transaction, Because the transaction is associated with a connection, every one of these commands must also be associated with the same connection instance. ADO.NET transactions do not support transactions across multiple connections; it is always a local transaction associated with one connection.
When y’0u create an object persistence model using multiple objects, for example, classes Course and Cours~Date, which should be persisted inside one transaction, it gets very difficult using-ADO.NET transactions. Here, it.js necessary to pass the transaction to all of the objects participating in the same transaction.
With Enterprise Services you get a lot of services for free. One of them is automatic transactions. Using transactions with System. EnterpriseServices has the advantage that it is not necessary to deal with transactions explicitly; transactions are automatically created by the runtime. Youjust have to add the attribute [Transaction] with the transactional requirements to the class. The [AutoComplete] attribute marks the method to automatically set the status bit for the transaction: if the method succeeds, the success bit is set, so the transaction can commit. If an exception happens, the transaction is,aborted.
using System. Diagnostics;
A big advantage of creating transactions with System. Enterp;iseservices is that multiple objects can easily run within the same transaction, and transactions are automatically enlisted. The disadvantages are that it requires the COM+ hosting model, and the class using the features of this technology must be derived from the base class ServicedComponent.
Enterprise ~ and using COM+ transactional services are covered in Chapter 44, “Enterprise Services.”