Use. NET Framework for Transaction Processing

Source: Internet
Author: User
Use. NET Framework for Transaction Processing
Author: zdnetWww.aspcool.comTime: 17:44:09 views:4636

After using ADO. Net for a while, you will find the difference in flexibility between the new dataset and ADO recordset. Ability to move forms and their relationships between layers, bind them to visual objects, and be able to use a set of standard classes to process what they are indeed.. NET Framework. But most business applications Program In the end, you must insert, update, or delete data in the backend database. When these operations occur, you need a way to ensure data integration. This is why transactions occur.



Most developers understand the basic concepts of acid attributes in transaction processing and a transaction process. Fundamentally, you need to ensure that all database modification operations should be completed in a specific transaction process; otherwise, they will cause problems. There are two basic methods to manage database transaction processes in. Net: either through an ADO. net connection object or through a COM + object.

Use ADO. net connection object



The connection objects in the system. Data. sqlclient and system. Data. oledbclient namespaces support transaction semantics. For example, you can manage the transaction context of a database connection as follows:


Dim myconnection as new sqlconnection (myconnstring)
Myconnection. open ()
Dim mycommand as new sqlcommand ()
Dim mytrans as sqltransaction
'Create the transaction Context
Mytrans = myconnection. begintransaction ()
'Assign both transaction object and Connection
'To command object for the transaction
Mycommand. Connection = myconnection
Mycommand. Transaction = mytrans



After any database modifications are performed Code Later, you can use mytrans. Commit () to commit this transaction or use the mytrans. rollback () command to roll back these changes.



There is a severe restriction on the transaction context of the connection object, that is, it can only be used for a single database (that is, the database you opened the connection ). Of course, you do not need to complete the database operation process from your own program. You can use the stored procedure of a specific database, which contains the transaction code. Although this can reduce your processing time, you are still limited to executing transaction processes in only one database. Control transaction processes on multiple databases

To control the transaction processes on multiple databases, you will have to use the transaction mechanism provided by COM +.



The system. deleiseservieces object provides a way to operate the COM + service through a unified namespace in. net. A part of the namespace includes the ability to manage transactions. To create a transaction of this type, you must first create an object that inherits the servicedcomponent object function from the system. isiseservices namespace. Your new object can then create and manage its own transaction context and take advantage of any resources with the transaction Resource Manager. Therefore, you can not only manage transactions between two SQL Server databases, but also the transaction process between SQL Server and an Oracle database. The following code snippet demonstrates the simple transaction process between two databases.


Imports system. Data. sqlclient
Imports system. enterpriseservices
'Use the transaction attribute for the common class trxcomponent
<Transaction (transactionoption. Required)> _
Public class trxcomponent

'Trxcomponent must inherit from the base class servicedcomponent
Inherits servicedcomponent

<AutoComplete ()> function adddata (ID as string) as string

'The code here inserts data into the first database

'The code here inserts data into the second database
End Function

End Class



Trxcomponent uses the COM + transaction service by inheriting the functions of the servicedcomponent component. After the autocomplete attribute of the adddata function is set, the database automatically submits the task at the end of the function. If the underlying database gives up its transaction processing process, the entire transaction processing process will also be abandoned. You can also manually control the transaction state by removing the autocomplete attribute and calling the setcomplete or setabort method of the contextutil object.



Conclusion



Database Transaction Management is an important tool for developers. It can maintain the consistency of an underlying database .. Net has its own mechanism for managing the transaction process of a single database, and there is also a set of bundle classes in the enterpriseservices namespace to help developers use the COM + transaction service without the need to do it in person.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.