473,788 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing Nested Transaction in C#

Hi,

I want to implement nested transactions in C#.
When I write BEGIN TRANSACTION inside another BEGIN TRANSACTION in an
SQL Script it works fine. But when I call BeginTransactio n() inside
another BeginTransactio n() in a c# code on same connection object it
throws exception as "SQLConnect ion doesnot support parallel
transaction".

Following is the code snippet that i have written.
----------------------------------------------------------------------------------------
// Create and open a connection
SQLConnection connection = new SqlConnection(" Initial
Catalog=mydatab ase; Data Source=mymachin e;Integrated Security=SSPI;" );
connection.Open ();

// Begin Outer Transaction
SQLTransaction transaction = connection.Begi nTransaction();

SQLTransaction innerTransactio n = null;
for (int i = 0; i < messageCount; ++i)
{
innerTransactio n = connection.Begi nTransaction();
// Do some stuff here eg execute update query
innerTransactio n.Commit();
}

// Commit outer transaction and close the connection
transaction.Com mit();
connection.Clos e();
----------------------------------------------------------------------------------------

Can anybody tell me how to implement nested transactions C#.

Mana

Sep 22 '06 #1
1 18873
This is acting as a save-point, yes? In which case, you could perhaps use
outerTransactio n.Save, probably with a fixed name each time so that you can
also use outerTransactio n.Rollback(theN ame) - but unless you are actually
using save-point rollback functionality, committing the inner transaction
doesn't do a whole lot anyway... so I'm guessing that in reality this is
from more complex code where the inner transaction is actually in a
sub-method that must work both inside and outside of an existing
transaction?

Well, if you are using 2.0, then how about using a TransactionScop e
(System.Transac tions assembly)? This deals with nesting for you even across
nested methods, and also supports distributed transactions. Note that unless
you use Sql-Server 2005 this will automatically create a DTC transaction
even for calls to a single database [on 2005 it uses the LTM to start with a
basic transaction and then promote to DTC if necessary].

In the following, the Commit() methods can be called either on individual
objects or the list, and should work happily with full transaction support
(e.g. where SomeObject.Comm it() executes multiple database commands):

class SomeObjectList : List<SomeObject >{
public void Commit() {
using (TransactionSco pe tran = new TransactionScop e()) {
foreach (SomeObject obj in this) {
obj.Commit();
}
tran.Complete() ;
}
}
}
class SomeObject {
public void Commit() {
using (TransactionSco pe tran = new TransactionScop e()) {
// obtain connection from pool and do some work
tran.Complete() ;
}
}
}
Sep 22 '06 #2

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

Similar topics

2
1731
by: Jason Godden | last post by:
Hi All, I'm in the process of moving a rather complicated plpgsql stored procedure to a C module and I'm curious about how to implement the for each row syntax in C. My understanding is that if you perform an SPI_exec whilst looping through the results of another exec the allocated SPITupleTable will be automatically unallocated. Is this correct or am I way off here and it's actually safe to iterate through the tuples in an...
0
4043
by: P. Emigh | last post by:
A client that synchronizes over the internet encountered Error #3003: "Could not start transaction; too many transactions already nested" when attempting to synchronize. I checked user groups and MS Knowledge Base and found nothing helpful. This didn't seem to be acknowledged as a synchroization error. Investigations revealed data errors that resulted in a cascade of related errors. Resolving the data errors apparently cured whatever...
0
1081
by: LBS | last post by:
Hello guys I'm trying to implement a wrapper class, and I'm havinf difficulty implementing the transaction. I have several classes. The wrapper WrapDbCommand, WrapConnection..etc, and the actual SQL server class. The connection, parameter,executes works fine. However, When I tries to create a transaction, my property always return
9
7619
by: John Sidney-Woollett | last post by:
Is it possible to use the dblink and dblink_exec features from inside pl/pgsql functions to mimic the behaviour of nested transactions by calling another function or executing some SQL via the dblink (into the same database)? Does the SQL statement executed within the dblink sit in its own isolated transaction/session? And would an error thrown in the dblink statement abort the enclosing session that initiated the call? What is the...
6
3188
by: Thapliyal, Deepak | last post by:
Hi, Assume I have a bank app.. When customer withdraws $10 from his accouint I have to do following --> update account_summary table --> update account detail_table Requirement: either both transactions should succeed or both transactions should be rolled back in case of failure.
2
1913
by: Andreas | last post by:
Hi, will I need "nested transactions" which - as I read - aren't implemented, yet ? I have some objects that rely on each other. Each has a status like proposal, working, canceled. table-A <--- table-B <--- table-C <--- table-D
0
1441
by: Pavel Stehule | last post by:
Hello I can't use nested transaction in perl DBI interface. I have CVS PostgreSQL. Propably DBI doesn't support last pg features. Can I set off DBI transaction control mechanism? Thank You Pavel Stehule
2
2315
by: Rodríguez Rodríguez, Pere | last post by:
Hello, The new release includes savepoints, but I need to do a begin inside another begin and a commit/rollback after another commit/rollback. For example, I have a pA procedure that update some data, so this procedure execute a begin, after manipulate data and ends with commit/rollback. pA procedure can to be called directly by the client aplication. In other hand, I have a pB procedure that also update some data and calls pA...
2
1957
by: Gerrit.Horeis | last post by:
Hi All, I have a problem with nested SQLQueries. I will give here an abstract sample of code which I wrote Method A { createSqlTransaction and call SQL Insert Statement including "Select ScopeIdentity();" try {
0
9498
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
10172
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
10110
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
9967
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...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
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...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.