473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

TransactionScope in framework 2.0

Hi,

I am new to 2.0 framework. I am trying to use TransactionScope in the
following code. I took four lables. Started TransactionScope scope.
After setting values to three labels I throw an exception. I expexted
all label value would remain as method Complete() of TransactionScope
scope was not complete. But actually three labels are showing new
values.

label1.Text="1";
label2.Text="1";
label3.Text="1";
label4.Text="1";
private void Change_Label()
{
using (TransactionScope scope = new TransactionScope())
{
Exception ee = new Exception("My Exception");
try
{
label1.Text = "0";
label2.Text = "0";
label3.Text = "0";
throw ee;
label4.Text = "0";
scope.Complete();
}
catch (Exception ex)
{
MessageBox.Show("Transaction not completed");
}
}
}

Can't we use TransactionScope in these situations? Is there alternate
solution? What if instead of statements, few methods were called each
operationg on different rows. Suppose I have three different method
m1(); m2(); m3() and insert one row each in table t1,t2 and t3
respectively. In a sample run after execution of m1() and m2() an
Exception occured. Would it mean no new row in t3 as well as t1 and
t2.

Thanks
Mukesh

May 29 '06 #1
6 1749
Mukesh,

AFAIK, TransactionScope is related to Database Transactions & not
memory transactions

Kalpesh

May 29 '06 #2
Hi Kalpesh,

Still the second part of the question is vaild where different methods
are updating tables. What are your ideas on it?

Regards,
Mukesh

May 29 '06 #3
AFAIK, when you include your objects in transaction scope CLR tries to enlist
these objects into TRM (transaction resource manager), in other words trying
to get info about what to do if your commit/abort transaction.

In your case, you use variables/functions that are not controlled by
TransactrionManagers (and there is no way to make them controlled), and in
the result you change you object directly.

This is well-known problem since 1997, where COM+ and Transactions firstly
come to our word. For this reason you need to use "Compensators". It's a way
to watch on you object that are used in transaction and in case of abort -
restore initial value. This idea used wide in COM+ world.
Another case, natural but forthcoming, is STM - Software Transaction Memmory
(googling to get more info). Microsoft have been working on this notion for
the last 3 years, but I'm not sure whether they have any samples.

Hi Kalpesh,

Still the second part of the question is vaild where different methods
are updating tables. What are your ideas on it?


--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

May 29 '06 #4
To test TransactionScope behaviour I created four table t1, t2, t3,t4
wil only one column sl. Four method m1, m2, m3 and m4 insert one row
to these tables respectively. These methods are independent otherwise.
There is a method Call_Methods() which will call m1, m2, m3 and m4
within transaction scope. m1 get executed. I checked this table from
Query analyzer. The row was locked. After calling m3 exception is
thrown and as exception handler TransactionScope is disposed. To my
surprise no changes was done to any tables. It means TransactionScope
automatically started transaction in each method. How it happened?
What if m2 uses m1 updated values?

private void Call_Methods()
{
using (TransactionScope scope = new TransactionScope())
{
Exception ee = new Exception("Mukesh created
exception");
try
{
m1();
m2();
m3();
throw ee;
m4();
scope.Complete();
}
catch (Exception ex)
{
MessageBox.Show("Exception Occured" + ex.Message);
scope.Dispose();
}
}
}
private void m1()
{
string conStr = "Persist Security Info=False;User
ID=sa;Password=931;Initial Catalog= mukesh; Data
Source=10.26.5.125\\LOCAL_MUKESH";
SqlConnection cn;
cn = new SqlConnection(conStr);
cn.Open();
SqlCommand cmd = new SqlCommand("Insert into t1 values(1)",
cn);
cmd.ExecuteNonQuery();
cn.Close();

}
private void m2()
{
string conStr = "Persist Security Info=False;User
ID=sa;Password=931;Initial Catalog= mukesh; Data
Source=10.26.5.125\\LOCAL_MUKESH";
SqlConnection cn;
cn = new SqlConnection(conStr);
cn.Open();
SqlCommand cmd = new SqlCommand("Insert into t2 values(2)",
cn);
cmd.ExecuteNonQuery();
cn.Close();

}
private void m3()
{
string conStr = "Persist Security Info=False;User
ID=sa;Password=931;Initial Catalog= mukesh; Data
Source=10.26.5.125\\LOCAL_MUKESH";
SqlConnection cn;
cn = new SqlConnection(conStr);
cn.Open();
SqlCommand cmd = new SqlCommand("Insert into t3 values(3)",
cn);
cmd.ExecuteNonQuery();
cn.Close();
}
private void m4()
{
string conStr = "Persist Security Info=False;User
ID=sa;Password=931;Initial Catalog= mukesh; Data
Source=10.26.5.125\\LOCAL_MUKESH";
SqlConnection cn;
cn = new SqlConnection(conStr);
cn.Open();
SqlCommand cmd = new SqlCommand("Insert into t4 values(4)",
cn);
cmd.ExecuteNonQuery();
cn.Close();
}

Thanks
Mukesh

May 30 '06 #5
Hello Mukesh,

It's because you methods use SqlConnection that can be enlisted in Transaction
Manager

I recomend to read this article http://www.microsoft.com/downloads/d...displaylang=en
in the beginning Juwal open this subject

M> To test TransactionScope behaviour I created four table t1, t2, t3,t4
M> wil only one column sl. Four method m1, m2, m3 and m4 insert one row
M> to these tables respectively. These methods are independent
M> otherwise.
M> There is a method Call_Methods() which will call m1, m2, m3 and m4
M> within transaction scope. m1 get executed. I checked this table
M> from
M> Query analyzer. The row was locked. After calling m3 exception is
M> thrown and as exception handler TransactionScope is disposed. To my
M> surprise no changes was done to any tables. It means
M> TransactionScope automatically started transaction in each method.
M> How it happened? What if m2 uses m1 updated values?
M>
M> private void Call_Methods()
M> {
M> using (TransactionScope scope = new TransactionScope())
M> {
M> Exception ee = new Exception("Mukesh created
M> exception");
M> try
M> {
M> m1();
M> m2();
M> m3();
M> throw ee;
M> m4();
M> scope.Complete();
M> }
M> catch (Exception ex)
M> {
M> MessageBox.Show("Exception Occured" +
M> ex.Message);
M> scope.Dispose();
M> }
M> }
M> }
M> private void m1()
M> {
M> string conStr = "Persist Security Info=False;User
M> ID=sa;Password=931;Initial Catalog= mukesh; Data
M> Source=10.26.5.125\\LOCAL_MUKESH";
M> SqlConnection cn;
M> cn = new SqlConnection(conStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Insert into t1
M> values(1)",
M> cn);
M> cmd.ExecuteNonQuery();
M> cn.Close();
M> }
M> private void m2()
M> {
M> string conStr = "Persist Security Info=False;User
M> ID=sa;Password=931;Initial Catalog= mukesh; Data
M> Source=10.26.5.125\\LOCAL_MUKESH";
M> SqlConnection cn;
M> cn = new SqlConnection(conStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Insert into t2
M> values(2)",
M> cn);
M> cmd.ExecuteNonQuery();
M> cn.Close();
M> }
M> private void m3()
M> {
M> string conStr = "Persist Security Info=False;User
M> ID=sa;Password=931;Initial Catalog= mukesh; Data
M> Source=10.26.5.125\\LOCAL_MUKESH";
M> SqlConnection cn;
M> cn = new SqlConnection(conStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Insert into t3
M> values(3)",
M> cn);
M> cmd.ExecuteNonQuery();
M> cn.Close();
M> }
M> private void m4()
M> {
M> string conStr = "Persist Security Info=False;User
M> ID=sa;Password=931;Initial Catalog= mukesh; Data
M> Source=10.26.5.125\\LOCAL_MUKESH";
M> SqlConnection cn;
M> cn = new SqlConnection(conStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Insert into t4
M> values(4)",
M> cn);
M> cmd.ExecuteNonQuery();
M> cn.Close();
M> }
M> Thanks
M> Mukesh
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
May 30 '06 #6
Hi,
The article is really very useful. Thanks a lot.

Mukesh

Michael Nemtsev wrote:
Hello Mukesh,

It's because you methods use SqlConnection that can be enlisted in Transaction
Manager

I recomend to read this article http://www.microsoft.com/downloads/d...displaylang=en
in the beginning Juwal open this subject

M> To test TransactionScope behaviour I created four table t1, t2, t3,t4
M> wil only one column sl. Four method m1, m2, m3 and m4 insert one row
M> to these tables respectively. These methods are independent
M> otherwise.
M> There is a method Call_Methods() which will call m1, m2, m3 and m4
M> within transaction scope. m1 get executed. I checked this table
M> from
M> Query analyzer. The row was locked. After calling m3 exception is
M> thrown and as exception handler TransactionScope is disposed. To my
M> surprise no changes was done to any tables. It means
M> TransactionScope automatically started transaction in each method.
M> How it happened? What if m2 uses m1 updated values?
M>
M> private void Call_Methods()
M> {
M> using (TransactionScope scope = new TransactionScope())
M> {
M> Exception ee = new Exception("Mukesh created
M> exception");
M> try
M> {
M> m1();
M> m2();
M> m3();
M> throw ee;
M> m4();
M> scope.Complete();
M> }
M> catch (Exception ex)
M> {
M> MessageBox.Show("Exception Occured" +
M> ex.Message);
M> scope.Dispose();
M> }
M> }
M> }
M> private void m1()
M> {
M> string conStr = "Persist Security Info=False;User
M> ID=sa;Password=931;Initial Catalog= mukesh; Data
M> Source=10.26.5.125\\LOCAL_MUKESH";
M> SqlConnection cn;
M> cn = new SqlConnection(conStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Insert into t1
M> values(1)",
M> cn);
M> cmd.ExecuteNonQuery();
M> cn.Close();
M> }
M> private void m2()
M> {
M> string conStr = "Persist Security Info=False;User
M> ID=sa;Password=931;Initial Catalog= mukesh; Data
M> Source=10.26.5.125\\LOCAL_MUKESH";
M> SqlConnection cn;
M> cn = new SqlConnection(conStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Insert into t2
M> values(2)",
M> cn);
M> cmd.ExecuteNonQuery();
M> cn.Close();
M> }
M> private void m3()
M> {
M> string conStr = "Persist Security Info=False;User
M> ID=sa;Password=931;Initial Catalog= mukesh; Data
M> Source=10.26.5.125\\LOCAL_MUKESH";
M> SqlConnection cn;
M> cn = new SqlConnection(conStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Insert into t3
M> values(3)",
M> cn);
M> cmd.ExecuteNonQuery();
M> cn.Close();
M> }
M> private void m4()
M> {
M> string conStr = "Persist Security Info=False;User
M> ID=sa;Password=931;Initial Catalog= mukesh; Data
M> Source=10.26.5.125\\LOCAL_MUKESH";
M> SqlConnection cn;
M> cn = new SqlConnection(conStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Insert into t4
M> values(4)",
M> cn);
M> cmd.ExecuteNonQuery();
M> cn.Close();
M> }
M> Thanks
M> Mukesh
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


May 31 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rick_Kierner | last post by:
I am attempting to use the TransactionScope class to manage my transactions. I am running into an issue. My web server is in an NT work group. My SQL Server is in a domain. When my code...
0
by: ste | last post by:
Please bear with this post.. its abit long winded.. but i thought it was important to describe what i am trying to achieve before i ask the questions (at the bottom) Thanks.....in advance ...
3
by: kikapu | last post by:
Hi to all folks, i am trying to understand the use of System.Transactions in general and TransactionScope particularly. What am i allowed to do in a using statement that wraps a...
4
by: hardieca | last post by:
Hi, I'd really like to use TransactionScope within the Business Layer of my web application. A book I'm reading makes a note that it should not be used in a shared web hosting environment...
0
by: RP | last post by:
I have a two classes, first named "ModCon" has procedures written for connections and the second named "ModRes" contains functions and procedures that can be reused. For my question it is important...
4
by: JT | last post by:
In .NET I have been using the TransactionScope class to wrap my unit tests into a transaction and roll back any database changes after I have completed my unit test. Now I am dealing with some...
3
by: Aleksey Timonin | last post by:
Hi guys, I tried to use TransactionScope on to defferent TableAdapters like this: using (TransactionScope transScope = new TransactionScope(TransactionScopeOption.Required)) {...
3
by: Michael Schöller | last post by:
Hello, First of all english is not my natural language so please fogive me some bad mistakes in gramatic and use of some vocables :). I have a great problem here. Well I will not use it...
2
by: GaryDean | last post by:
When I use transactions with sql server I usually do this... using (TransactionScope scope = new TransactionScope) { using (SqlConnection1 = new SqlConnection . . . . . and this all works...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.