473,473 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Handling concurrency violations

Hi

I have developed the following logic to handle db concurrency violations. I
just wonder if someone can tell me if it is correct or if I need a
different approach.Would love to know how pros handle it.

Thanks

Regards
Dim dc As DataColumn
Dim drCache As DataRow
Dim drCurrent As DataRow

Try
' Attempt the update
daContacts.Update(ds.Contacts)

Catch Ex As DBConcurrencyException

' First - cache the row
drCache = ds.Contacts.NewRow()
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCache(dc.ColumnName) = Ex.Row(dc.ColumnName)
End If
Next

' Refresh from database
daContacts.Fill(ds.Contacts)

' Position to the faulted row
drCurrent = ds.Contacts.Rows.Find(Ex.Row("ID"))

' Apply User Changes
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCurrent(dc.ColumnName) = drCache(dc.ColumnName)
End If
Next

' Save again
daContacts.Update(ds.Contacts)
End Try
Mar 26 '08 #1
5 1825
John,

It looks like your approach to concurrency handling is to get around the
concurrency error by reloading the data from the database and then applying
your changes.

You might as well just turn off concurrency checking, since that is the end
result anyway.

In other words, it looks like your code is just avoiding concurrency
exceptions, not handling concurrency conflicts.

Kerry Moorman

"John" wrote:
Hi

I have developed the following logic to handle db concurrency violations. I
just wonder if someone can tell me if it is correct or if I need a
different approach.Would love to know how pros handle it.

Thanks

Regards
Dim dc As DataColumn
Dim drCache As DataRow
Dim drCurrent As DataRow

Try
' Attempt the update
daContacts.Update(ds.Contacts)

Catch Ex As DBConcurrencyException

' First - cache the row
drCache = ds.Contacts.NewRow()
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCache(dc.ColumnName) = Ex.Row(dc.ColumnName)
End If
Next

' Refresh from database
daContacts.Fill(ds.Contacts)

' Position to the faulted row
drCurrent = ds.Contacts.Rows.Find(Ex.Row("ID"))

' Apply User Changes
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCurrent(dc.ColumnName) = drCache(dc.ColumnName)
End If
Next

' Save again
daContacts.Update(ds.Contacts)
End Try
Mar 26 '08 #2
So Kerry how do I handle it? which was my original question.

Regards
"Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:31**********************************@microsof t.com...
John,

It looks like your approach to concurrency handling is to get around the
concurrency error by reloading the data from the database and then
applying
your changes.

You might as well just turn off concurrency checking, since that is the
end
result anyway.

In other words, it looks like your code is just avoiding concurrency
exceptions, not handling concurrency conflicts.

Kerry Moorman

"John" wrote:
>Hi

I have developed the following logic to handle db concurrency violations.
I
just wonder if someone can tell me if it is correct or if I need a
different approach.Would love to know how pros handle it.

Thanks

Regards
Dim dc As DataColumn
Dim drCache As DataRow
Dim drCurrent As DataRow

Try
' Attempt the update
daContacts.Update(ds.Contacts)

Catch Ex As DBConcurrencyException

' First - cache the row
drCache = ds.Contacts.NewRow()
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCache(dc.ColumnName) = Ex.Row(dc.ColumnName)
End If
Next

' Refresh from database
daContacts.Fill(ds.Contacts)

' Position to the faulted row
drCurrent = ds.Contacts.Rows.Find(Ex.Row("ID"))

' Apply User Changes
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCurrent(dc.ColumnName) = drCache(dc.ColumnName)
End If
Next

' Save again
daContacts.Update(ds.Contacts)
End Try

Mar 26 '08 #3
John,

You need to decide how you want to handle concurrency issues in a particular
application.

For example, how do you want to handle this situation:

Mary loads your employee record from the Employees table into the Employee
Manager application. She loaded your record because your cell phone number
has changed and she is updating that piece of information in your record.

While Mary is working on your record, Jim loads your employee record from
the Employees table into the Employee Manager application. He loaded your
record because you have just gotten a big raise and he is updating that piece
of information in your record. He changes your salary and updates your record
in the database.

Mary has now finished changing your cell phone number and attempts to update
your record in the database.

Do you want Mary's update to succeed? If it does you have just lost your
raise because her update will overwrite your new salary that Jim entered with
your old salary as it was when Mary loaded your record.

Do you want Mary's update to fail because of a concurrency conflict? When it
fails, how do you want to deal with the conflict?

Your current code appears to read the updated record and then apply the old
values to it. This gets rid of the concurrency conflict but you still end up
with your old salary instead of your raise.

My choice would be to inform the user that the record has been changed by
another user and let the user reload the record and start over.

Kerry Moorman

"John" wrote:
So Kerry how do I handle it? which was my original question.

Regards
"Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:31**********************************@microsof t.com...
John,

It looks like your approach to concurrency handling is to get around the
concurrency error by reloading the data from the database and then
applying
your changes.

You might as well just turn off concurrency checking, since that is the
end
result anyway.

In other words, it looks like your code is just avoiding concurrency
exceptions, not handling concurrency conflicts.

Kerry Moorman

"John" wrote:
Hi

I have developed the following logic to handle db concurrency violations.
I
just wonder if someone can tell me if it is correct or if I need a
different approach.Would love to know how pros handle it.

Thanks

Regards
Dim dc As DataColumn
Dim drCache As DataRow
Dim drCurrent As DataRow

Try
' Attempt the update
daContacts.Update(ds.Contacts)

Catch Ex As DBConcurrencyException

' First - cache the row
drCache = ds.Contacts.NewRow()
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCache(dc.ColumnName) = Ex.Row(dc.ColumnName)
End If
Next

' Refresh from database
daContacts.Fill(ds.Contacts)

' Position to the faulted row
drCurrent = ds.Contacts.Rows.Find(Ex.Row("ID"))

' Apply User Changes
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCurrent(dc.ColumnName) = drCache(dc.ColumnName)
End If
Next

' Save again
daContacts.Update(ds.Contacts)
End Try


Mar 26 '08 #4
Hi Kerry

Many thanks for this insight. Is there an easy way to display both new and
old row values to the user to let him/her select?

Thanks

Regards

"Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:28**********************************@microsof t.com...
John,

You need to decide how you want to handle concurrency issues in a
particular
application.

For example, how do you want to handle this situation:

Mary loads your employee record from the Employees table into the Employee
Manager application. She loaded your record because your cell phone number
has changed and she is updating that piece of information in your record.

While Mary is working on your record, Jim loads your employee record from
the Employees table into the Employee Manager application. He loaded your
record because you have just gotten a big raise and he is updating that
piece
of information in your record. He changes your salary and updates your
record
in the database.

Mary has now finished changing your cell phone number and attempts to
update
your record in the database.

Do you want Mary's update to succeed? If it does you have just lost your
raise because her update will overwrite your new salary that Jim entered
with
your old salary as it was when Mary loaded your record.

Do you want Mary's update to fail because of a concurrency conflict? When
it
fails, how do you want to deal with the conflict?

Your current code appears to read the updated record and then apply the
old
values to it. This gets rid of the concurrency conflict but you still end
up
with your old salary instead of your raise.

My choice would be to inform the user that the record has been changed by
another user and let the user reload the record and start over.

Kerry Moorman

"John" wrote:
>So Kerry how do I handle it? which was my original question.

Regards
"Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:31**********************************@microso ft.com...
John,

It looks like your approach to concurrency handling is to get around
the
concurrency error by reloading the data from the database and then
applying
your changes.

You might as well just turn off concurrency checking, since that is the
end
result anyway.

In other words, it looks like your code is just avoiding concurrency
exceptions, not handling concurrency conflicts.

Kerry Moorman

"John" wrote:

Hi

I have developed the following logic to handle db concurrency
violations.
I
just wonder if someone can tell me if it is correct or if I need a
different approach.Would love to know how pros handle it.

Thanks

Regards
Dim dc As DataColumn
Dim drCache As DataRow
Dim drCurrent As DataRow

Try
' Attempt the update
daContacts.Update(ds.Contacts)

Catch Ex As DBConcurrencyException

' First - cache the row
drCache = ds.Contacts.NewRow()
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCache(dc.ColumnName) = Ex.Row(dc.ColumnName)
End If
Next

' Refresh from database
daContacts.Fill(ds.Contacts)

' Position to the faulted row
drCurrent = ds.Contacts.Rows.Find(Ex.Row("ID"))

' Apply User Changes
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCurrent(dc.ColumnName) = drCache(dc.ColumnName)
End If
Next

' Save again
daContacts.Update(ds.Contacts)
End Try



Mar 26 '08 #5
John,

I don't display both new and old values to the user, so I don't have any
code examples.

However, this book has a section on just that topic. I can highly recommend
this book in general:

Programming Microsoft ADO.NET 2.0 Core Reference by David Sceppa

Kerry Moorman
"John" wrote:
Hi Kerry

Many thanks for this insight. Is there an easy way to display both new and
old row values to the user to let him/her select?

Thanks

Regards

"Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:28**********************************@microsof t.com...
John,

You need to decide how you want to handle concurrency issues in a
particular
application.

For example, how do you want to handle this situation:

Mary loads your employee record from the Employees table into the Employee
Manager application. She loaded your record because your cell phone number
has changed and she is updating that piece of information in your record.

While Mary is working on your record, Jim loads your employee record from
the Employees table into the Employee Manager application. He loaded your
record because you have just gotten a big raise and he is updating that
piece
of information in your record. He changes your salary and updates your
record
in the database.

Mary has now finished changing your cell phone number and attempts to
update
your record in the database.

Do you want Mary's update to succeed? If it does you have just lost your
raise because her update will overwrite your new salary that Jim entered
with
your old salary as it was when Mary loaded your record.

Do you want Mary's update to fail because of a concurrency conflict? When
it
fails, how do you want to deal with the conflict?

Your current code appears to read the updated record and then apply the
old
values to it. This gets rid of the concurrency conflict but you still end
up
with your old salary instead of your raise.

My choice would be to inform the user that the record has been changed by
another user and let the user reload the record and start over.

Kerry Moorman

"John" wrote:
So Kerry how do I handle it? which was my original question.

Regards
"Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:31**********************************@microsof t.com...
John,

It looks like your approach to concurrency handling is to get around
the
concurrency error by reloading the data from the database and then
applying
your changes.

You might as well just turn off concurrency checking, since that is the
end
result anyway.

In other words, it looks like your code is just avoiding concurrency
exceptions, not handling concurrency conflicts.

Kerry Moorman

"John" wrote:

Hi

I have developed the following logic to handle db concurrency
violations.
I
just wonder if someone can tell me if it is correct or if I need a
different approach.Would love to know how pros handle it.

Thanks

Regards
Dim dc As DataColumn
Dim drCache As DataRow
Dim drCurrent As DataRow

Try
' Attempt the update
daContacts.Update(ds.Contacts)

Catch Ex As DBConcurrencyException

' First - cache the row
drCache = ds.Contacts.NewRow()
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCache(dc.ColumnName) = Ex.Row(dc.ColumnName)
End If
Next

' Refresh from database
daContacts.Fill(ds.Contacts)

' Position to the faulted row
drCurrent = ds.Contacts.Rows.Find(Ex.Row("ID"))

' Apply User Changes
For Each dc In ds.Contacts.Columns
If Not dc.ReadOnly Then
drCurrent(dc.ColumnName) = drCache(dc.ColumnName)
End If
Next

' Save again
daContacts.Update(ds.Contacts)
End Try



Mar 27 '08 #6

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

Similar topics

4
by: john | last post by:
How do u guys handle multiple sessions?? i.e, opening different browser windows by running iexplore.exe or clicking IE icons and opening the application. My sessions are mixing up. what i mean is...
2
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data...
13
by: mfreeman | last post by:
The minimal code (VB.NET 2003) needed to show this problem is shown below. All I do is loop through the records in the table and update them without making any changes. Out of 600 records, about...
4
by: Rob | last post by:
Hey all, So.. a simple FormView/SqlDataSource to handle inserting records into a table. The table has a primary key that the user enters (eg DiscountCode). If the user enters a duplicate the...
4
by: Bob | last post by:
While testing my my program I came up with a consistency exception. My program consists of three datagridviews, One called dgvPostes which is the parent grid and its two children,one called...
1
by: John | last post by:
Hi I have a perfectly working vs 2003 winform data app. All the data access code has been generated using the data adapter wizard and then pasted into the app. I had to add a new field in the...
7
by: John | last post by:
Hi I have asked this question before but have not received any clear answer. I have a winform app with bound controls via dataadapter/dataset combination.I have a dbconcurrency exception ex. Now...
37
by: Sweetiecakes | last post by:
Hello I'm a bit confused on how one should handle exceptions. I'm currently building an ADO.NET Windows Forms application. I've built a class for manipulating data within the database in...
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
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
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.