Home > Articles

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Automatic Web Service Transactions Are Easy

You let the compiler know that you want to use an automatic transaction declaratively—by adding a TransactionOption property to the WebMethod attribute. The following WebMethod attribute creates a new transaction for the method's use:

C#:

[WebMethod(TransactionOption=TransactionOption.RequiresNew)]
public void DoSomething()
{
 // Do some stuff here that requires a transaction...
}

VB:

<WebMethod(TransactionOption := TransactionOption.RequiresNew)> _
Public Function SoSomething()
 ' Do some stuff here that requires a transaction...
End Function

There are other TransactionOption choices besides RequiresNew, as shown in Table 2. We'll be using only RequiresNew in the examples in this article.

Table 2 TransactionOption Choices

Attribute

Description

Syntax

Disabled

The web service method doesn't run within the scope of a transaction. When a request is processed, the web service method is executed without a transaction.

[WebMethod(TransactionOption=

TransactionOption.Disabled)]

NotSupported

The web service method doesn't run within the scope of a transaction. When a request is processed, the web service method is executed without a transaction.

[WebMethod(TransactionOption=

TransactionOption.NotSupported)]

Supported

The web service method doesn't run within the scope of a transaction. When a request is processed, the web service is created without a transaction.

[WebMethod(TransactionOption=

TransactionOption.Supported)]

Required

The web service method requires a transaction. Since web service methods can only participate as the root object in a transaction, a new transaction will be created for the web service method.

[WebMethod(TransactionOption=

TransactionOption.Required)]

RequiresNew

The web service method requires a new transaction. When a request is processed, the web service is created within a new transaction.

[WebMethod(TransactionOption=

TransactionOption.RequiresNew)]


When you perform database operations with an automatic web service transaction, you don't necessarily have to catch exceptions. If an exception is thrown and you don't catch it, the transaction is aborted and the exception is passed back to the calling code.

  • + Share This
  • 🔖 Save To Your Account