Differences Between Below Using Statement

Posted by Jayakumars under ASP.NET AJAX on 10/30/2013 | Points: 10 | Views : 1955 | Status : [Member] [MVP] | Replies : 7
Hi

Why we use using SqlConnection?

Differences Between Below Using Statement

1.using (System.Transactions.TransactionScope Trans1=new System.Transactions(TransactionScopeOption.Required)

2.using (System.Transactions.TransactionScope Trans2=new System.Transactions(TransactionScopeOption.RequiresNew)

3.using (System.Transactions.TransactionScope Trans3=new System.Transactions(TransactionScopeOption.Suppress)

Mark as Answer if its helpful to you


Responses

Posted by: Bandi on: 10/30/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Required: A transaction is required by the scope. It uses an ambient transaction if one already exists. Otherwise, it creates a new transaction before entering the scope. This is the default value.
RequiresNew: A new transaction is always created for the scope.
Suppress: The ambient transaction context is suppressed when creating the scope. All operations within the scope are done without an ambient transaction context.

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/30/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
References:
http://msdn.microsoft.com/en-us/library/ms172152%28v=vs.90%29.aspx#Y1642
http://stackoverflow.com/questions/4629245/transactionscopeoption-required-or-requiresnew

To understand nested transaction scopes:
http://stackoverflow.com/questions/6987862/understanding-transactionscopeoptions-requiresnew-suppress-required
http://web.archive.org/web/20100829210742/http://www.pluralsight-training.net/community/blogs/jimjohn/archive/2005/06/18/11451.aspx

--Sample
using ( var t1 = new TransactionScope( TransactionScopeOption.Required ) )

{
//MyStuff

using ( var t2 = new TransactionScope( TransactionScopeOption.Suppress) )
{
//MyStuff

using ( var t3 = new TransactionScope( TransactionScopeOption.Required) )
{
//Mystuff

t3.Complete();
}

t2.Complete();
}

t1.Complete();
}


Explanation:
t3 Transaction does the inner TransactionScope take as ambient transaction.
There is nothing else to choose from there, since the scope of t2 is to supress t1, not to create it's own ambient. Therefore at the innermost scope there is only t3 and nothing else.

If t2 would be RequireNew then the innermost scope ambient would be t2 since t3 would join t2.


An stricle related to TransactionScopes:
http://blogs.msdn.com/b/angelsb/archive/2005/02/12/371557.aspx?Redirected=true

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 10/30/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
bandi

i use the command below method

using (System.Transactions.TransactionScope TransBuy = new System.Transactions.TransactionScope(TransactionScopeOption.RequiresNew))
{
string Geno=Classname.Method name
}

I am facing this error how to solve this

error
====

The partner transaction manager has disabled its support for remote/network transactions.
(Exception from HRESULT: 0x8004D025)

Mark as Answer if its helpful to you

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/30/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
I haven't used this type of transactions...

By looki1) ng into several examples on this... I figured out that there should be second parameter transactionOptions

using (System.Transactions.TransactionScope TransBuy = new System.Transactions.TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() )


2) Most of the people got solution by enabling Network DTC ( follow this link)
http://support.microsoft.com/kb/817064

After setting up the above DTC reboot the system...
Verify DTC Service is running on both database server computer and client computers..

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/30/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link especially for "Steps of creating distributed transaction "
http://www.c-sharpcorner.com/UploadFile/mosessaur/TransactionScope04142006103850AM/TransactionScope.aspx

Did you open sql conncetion with database in side using(TransactionScope ....) { //Conncetion?}
Did you have transactionScope.Complete() ; statement ?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: JAYAKUMARS on: 10/30/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
bandi

always your response good thanks but can u post which transaction type is best
in C# and sql server with 3 tier Architecture why? can you post that source.

Mark as Answer if its helpful to you

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/31/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Jayakumar,
Simply we will use Using(Transaction ....) statement and try...catch statemetns to handle commit and rollbacks


Refer this link for SQL Server transaction types...
http://www.c-sharpcorner.com/UploadFile/6897bc/sql-server-transaction-types/


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response