473,480 Members | 4,841 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to ensure thread-safety between inter-dependent database persistence managers.

Hi all,
Assume we have two entity class.

Class1:
Name: House
Property:
ID:int
Name:String
Desktops:Desktop[]

Class2:
Name: Desktop

And we have two corresponding Database Managers: HouseDBManager and
DesktopManager

If we want to insert a House instance into database, we need to call
HouseDBManager#Insert(house).
And the general step is inserting ID and Name into database, then insert
Desktops though DesktopManager.

HouseDBManager#Insert(House house){
Execute "Insert into House values(ID,NAME)"

foreach ( Desktop desktop in house.Desktops ){
desktopManager.Insert(desktop);
}
}

My question is how to ensure thread-safety? If the house holds five
desktops, inserting the first 4 runs successfully, but inserting the last
one fails. Since inserting desktop is achieved by DesktopManager,
transaction will already be committed after inserting each desktop, how can
we rollback the inserting on the first four desktops.

Maybe sharing an IDbTransaction and IDbCommand between different managers is
a feasible solution. But it looks strange and I want to know whether there
is better solution to achieve this goal.

Thank you.
Nov 17 '06 #1
4 1737
A better approach might be TransactionScope in 2.0; this will give you
serializable isolation (by default) [providing thread safety at the database
layer], and no need to pass the transaction / connection objects around;

just use:

using(TransactionScope ts = new TransactionScope()) {
// your code snippet
ts.Complete();
}

or whatever the actual method is. You will need to manage your own rollback
to any IDs; I do this by bundling all this up into a helpper class that
snapshots (think: memento) the IDs / timstamps of each entity (via a simple
interface) so that it can reapply them if it fails.

Note that on SqlServer 2005 this uses a promotable transaction, so it is
very efficient for a single database; for 2000 it uses the DTC immediately
(even for single database) - but hey! it works!

Marc
Nov 17 '06 #2
Hi,

Your question isn't about thread-safety, it's about transactions.

You must either pass a transaction object to each method or create a global
transaction context.

If you're using the 2.0 framework, check out the System.Transactions
namespace. In particular, the TransactionScope class already works with
ADO.NET to provide global transaction support. Commands auto-enlist in the
global transaction (this can be prevented in the connection string but
auto-enlist is true by default).

using (TransactionScope scope = new TransactionScope())
{
HouseDBManager.Insert(house);
scope.Complete();
}

In earlier framework versions you can use COM+ transactions but they might be
more costly depending on your RDBMS. (If your're using Sql Server 2005 then
TransactionScope will be light-weight, unless it needs to be promoted to a
distributed transaction, which is done automatically).

"Features Provided by System.Transactions"
http://msdn2.microsoft.com/en-us/library/0abf6ykb.aspx

--
Dave Sexton

"news.microsoft.com" <zl***@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi all,
Assume we have two entity class.

Class1:
Name: House
Property:
ID:int
Name:String
Desktops:Desktop[]

Class2:
Name: Desktop

And we have two corresponding Database Managers: HouseDBManager and
DesktopManager

If we want to insert a House instance into database, we need to call
HouseDBManager#Insert(house).
And the general step is inserting ID and Name into database, then insert
Desktops though DesktopManager.

HouseDBManager#Insert(House house){
Execute "Insert into House values(ID,NAME)"

foreach ( Desktop desktop in house.Desktops ){
desktopManager.Insert(desktop);
}
}

My question is how to ensure thread-safety? If the house holds five
desktops, inserting the first 4 runs successfully, but inserting the last
one fails. Since inserting desktop is achieved by DesktopManager,
transaction will already be committed after inserting each desktop, how can
we rollback the inserting on the first four desktops.

Maybe sharing an IDbTransaction and IDbCommand between different managers is
a feasible solution. But it looks strange and I want to know whether there
is better solution to achieve this goal.

Thank you.

Nov 17 '06 #3
own rollback to IDs
Meaning: to your entity objects; the database will roll itself back happily

Marc
Nov 17 '06 #4
zlf
Got it. Thank you:)
Nov 17 '06 #5

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

Similar topics

2
322
by: Rene_Muench | last post by:
Hi there, I have listed 12 month on a continous form, where as I flag the current month with a tick box. To ensure at only 1 tick box is check not more or all I would want to validate and...
6
1179
by: Rob Lynch | last post by:
Sorry for the duplicate, but I wanted to make sure this would be seen by MS, and that they would answer it.... I have noticed (by getting ignored) that they won't just answer any old email and I want...
3
1062
by: Ray Cassick \(Home\) | last post by:
I have several classes that has a public interface (nothing really different there :) ). 1) I would like to ensure that some classes can only be instantiated by a specific class type. 2) ...
1
11732
by: Dhruba Bandopadhyay | last post by:
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm....
15
11365
by: geskerrett | last post by:
I am sure this is a basic math issue, but is there a better way to ensure an int variable is divisible by 4 than by doing the following; x = 111 x = (x /4) * 4 Just seems a bit clunky to me.
7
1253
by: David | last post by:
Hi using C#, .NET 1.1 I want to ensure a particular control is in a certain place in the page... For example, in the head section, I want to ensure that a literal control of a certain name...
6
1518
by: Charles Cain | last post by:
How can i ensure that i have received a file completely from an external source before reading it?Suppose i am receiving a file from an external source and i try to read the file before i have...
4
1858
by: MLH | last post by:
How do I ensure my reports are being printed in max resolution of 1200dpi ? That is, can Access control the setting?
8
1395
by: Markus | last post by:
Hello everyone. Recently I stumbled upon an interesting problem related to thread-parallel programming in C (and similarily C++). As an example assume a simple "buffer" array of size 8, e.g....
1
1436
by: lathamoulali | last post by:
Hello frnds, I am new to ASP Programming,please help me out with this problem... The site uses the same ASP file but the map is generated when using the anonymous user and other times the map...
0
7041
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,...
0
7080
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...
1
6736
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...
0
6908
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...
1
4772
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...
0
4478
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...
0
2994
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...
0
1299
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 ...
1
561
muto222
php
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.