What are Transactions?
A transaction symbolizes code or a set of components or procedures that must be executed as a unit. All the methods must execute successfully or the complete unit fails. A transaction can be described to cover the ACID properties for mission-critical applications.
What are the ACID Properties?
- Atomicity
- Consistency
- Isolation
- Durability
Transfer Funds Sample
We will build a sample ASP.NET web form for a fictitious bank that will transfer a specified amount from one account to another - if the balance in the first account is sufficient to cover the transfer.
First, we need to create the database we will use in the example.
I used an MS Access database containing only one table: tblAccount.
Field name |
Field type |
AccNumber |
Text |
dBalance |
Double |
Listing 1 displays the code for the web page. Save the web page as Test.aspx.
First include the Namespaces required for accessing the data.
Here is the function which processes the transaction for transferring the data.
For this example, we assume that the transaction should be rolled back (Cancelled) if.
- There are insufficient funds in the From Account to cover the transfer.
- Either of the SQL statements for marking credit or debit in the To and From accounts results in an error.
We create the Connection needed to connect to our database.
In real life, we would use a Server.MapPath to map to the location of the database.
We use the Data Reader reader to check the validity of the amount in the From Account. The crux of the function is to execute the two SQL queries one to subtract the amount from the From Account and one to add the same amount to the balance in the To Account.
We start the transaction after we have created the data objects. The transaction should be kept as short as possible to avoid concurrency issues and to enable a maximum number of positive commits.
Create the transaction and associate the transaction with the OleDbCommand as follows.
Within the Try block run the transaction and Commit the transaction if everything proceeds smoothly. Committing a transaction will write the changes to the database.
If there is an exception we will Roll Back the transaction. This will cancel any changes that have been carried out as a part of the transaction. This is how we maintain the integrity of our transactions.
try
Note. how do we Throw an exception if the balance in the From Account is less than the transfer amount?
The string passed in the constructor of the Exception object initializes the message for the Exception that will be raised.
Finally, we indicate the results of the transfer activity to the user.
OR
In real life, we would have converted the error message to a more meaningful and user-friendly message.
Here is the complete code listing for the web form.
Listing 1.Test.aspx: Transfer Funds Web Page.
![Fictitious Bank]()
Figure 1: Front-end web page for the transaction example.
![Transfer funds]()
Figure 2. Successfully Committed Transactions.
![Microsoft internet]()
Figure 3. Insufficient Funds RollBack.
Note. When the transaction is rolled back (Insufficient funds or an error in the SQL statements) the Balance field in both the From Account and To Account in the database is not updated.
Conclusion
Real-time web projects will need an extensive use of transactions. The principle is All or None.