TransactionScope in framework 2.0

M

Mukesh

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
 
K

Kalpesh

Mukesh,

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

Kalpesh
 
M

Mukesh

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
 
G

Guest

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
 
M

Mukesh

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
 
M

Michael Nemtsev

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/...73-BC4E-4C14-AF25-0F32AE3C73A0&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 Nietzsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top