473,750 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TransactionScop e in framework 2.0

Hi,

I am new to 2.0 framework. I am trying to use TransactionScop e in the
following code. I took four lables. Started TransactionScop e scope.
After setting values to three labels I throw an exception. I expexted
all label value would remain as method Complete() of TransactionScop e
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 (TransactionSco pe scope = new TransactionScop e())
{
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 ("Transactio n not completed");
}
}
}

Can't we use TransactionScop e 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 1773
Mukesh,

AFAIK, TransactionScop e 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
TransactrionMan agers (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 "Compensato rs". 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 TransactionScop e 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 TransactionScop e is disposed. To my
surprise no changes was done to any tables. It means TransactionScop e
automatically started transaction in each method. How it happened?
What if m2 uses m1 updated values?

private void Call_Methods()
{
using (TransactionSco pe scope = new TransactionScop e())
{
Exception ee = new Exception("Muke sh 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_MUKE SH";
SqlConnection cn;
cn = new SqlConnection(c onStr);
cn.Open();
SqlCommand cmd = new SqlCommand("Ins ert into t1 values(1)",
cn);
cmd.ExecuteNonQ uery();
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_MUKE SH";
SqlConnection cn;
cn = new SqlConnection(c onStr);
cn.Open();
SqlCommand cmd = new SqlCommand("Ins ert into t2 values(2)",
cn);
cmd.ExecuteNonQ uery();
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_MUKE SH";
SqlConnection cn;
cn = new SqlConnection(c onStr);
cn.Open();
SqlCommand cmd = new SqlCommand("Ins ert into t3 values(3)",
cn);
cmd.ExecuteNonQ uery();
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_MUKE SH";
SqlConnection cn;
cn = new SqlConnection(c onStr);
cn.Open();
SqlCommand cmd = new SqlCommand("Ins ert into t4 values(4)",
cn);
cmd.ExecuteNonQ uery();
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 TransactionScop e 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 TransactionScop e is disposed. To my
M> surprise no changes was done to any tables. It means
M> TransactionScop e 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 (TransactionSco pe scope = new TransactionScop e())
M> {
M> Exception ee = new Exception("Muke sh 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_MUKE SH";
M> SqlConnection cn;
M> cn = new SqlConnection(c onStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Ins ert into t1
M> values(1)",
M> cn);
M> cmd.ExecuteNonQ uery();
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_MUKE SH";
M> SqlConnection cn;
M> cn = new SqlConnection(c onStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Ins ert into t2
M> values(2)",
M> cn);
M> cmd.ExecuteNonQ uery();
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_MUKE SH";
M> SqlConnection cn;
M> cn = new SqlConnection(c onStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Ins ert into t3
M> values(3)",
M> cn);
M> cmd.ExecuteNonQ uery();
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_MUKE SH";
M> SqlConnection cn;
M> cn = new SqlConnection(c onStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Ins ert into t4
M> values(4)",
M> cn);
M> cmd.ExecuteNonQ uery();
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 TransactionScop e 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 TransactionScop e is disposed. To my
M> surprise no changes was done to any tables. It means
M> TransactionScop e 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 (TransactionSco pe scope = new TransactionScop e())
M> {
M> Exception ee = new Exception("Muke sh 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_MUKE SH";
M> SqlConnection cn;
M> cn = new SqlConnection(c onStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Ins ert into t1
M> values(1)",
M> cn);
M> cmd.ExecuteNonQ uery();
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_MUKE SH";
M> SqlConnection cn;
M> cn = new SqlConnection(c onStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Ins ert into t2
M> values(2)",
M> cn);
M> cmd.ExecuteNonQ uery();
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_MUKE SH";
M> SqlConnection cn;
M> cn = new SqlConnection(c onStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Ins ert into t3
M> values(3)",
M> cn);
M> cmd.ExecuteNonQ uery();
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_MUKE SH";
M> SqlConnection cn;
M> cn = new SqlConnection(c onStr);
M> cn.Open();
M> SqlCommand cmd = new SqlCommand("Ins ert into t4
M> values(4)",
M> cn);
M> cmd.ExecuteNonQ uery();
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
2055
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 attempts to execute the Complete method of the TransactionScope object, I get the following error: "The transaction has already been implicitly or explicitly committed or aborted" I have tried altering the registry entry to...
0
1959
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 Steve Okay.. here goes..................
3
7721
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 TransactionScope object, so this to be rollback in case of an exception or when i do not call scope.complete ?? I mean, can i set an object's variables and then un-done these sets
4
5555
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 because the server may get re-configured. Can anyone elaborate on this? Regards, Chris
0
1439
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 to add sample codes of the two classes and later the code from a Form's button click event which is giving problem. ======================================== public string ConString; public SqlConnection myCN = new SqlConnection();
4
3505
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 JAVA developers and looking for a similar way for them to be able to do this from their JAVA code and not at the database level. Is there and equivalent to this in JAVA? Thanks.
3
8026
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)) { TableAdapter1.InsertQuery(id, name); TableAdapter2.InsertQuery(id, customerName, customerAddress); transScope.Complete(); }
3
2896
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 anymore but I want to know why it is as it is ^^. I tried with .NET3.0 but I think it will be the same with 2.0 and 3.5. MSDTC is configured and working.
2
2635
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 great. But now, I need to stretch the transaction across to sql databases using two different connections i.e. using (TransactionScope scope = new TransactionScope) {
0
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9394
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.