concurrency question

  • Thread starter Thread starter William E Voorhees
  • Start date Start date
W

William E Voorhees

I'm updating an Access database in a windows multi-user environment.

I'm using disconnected data

I read data from an Access Data table to a data object

I update the data object from a windows form

I save the data from the data object to the Access Data table using a data
adapter as follows:



adpTeam.UpdateCommand = New OleDbCommand(sqlUpdate)

adpTeam.UpdateCommand.Connection = aConnection

adpTeam.UpdateCommand.ExecuteNonQuery()



Since I'm not using datasets, how do I use optimistic concurrency and throw
a concurrency exception if one occurs?
 
If your sql update statement was written correctly using either the
verson number or saving-all-values approach then the
adpTeam.UpdateCommand.ExecuteNonQuery()
statement should raise a DBConcurrencyException when the number of rows
affected by the update is zero which is generally a concurrency
violation.

In this way, you don't have to explicitly raise a concurrency
exception.

Andy
 
I have a class containing public shared variables to hold the originally
read fields from the Team Table. (2 fields)

I use the sql string listed below to update the description field. It does
update the database when no concurrency problem occurs. If a concurrency
does occur, the update does not occur because the Where Clause prevents it,
but no exception is thrown. I have the update in a try / catch block but no
exceptions are caught.

I'm not sure why. It would seem that the numbers of rows affected by the
update would be 0.

Dim sqlUpdate As String = "Update TeamTable " & _

"Set TeamTable.Description = '" & strDescription & "' " & _

"Where TeamTable.ID = '" & cOrigTeam.strTeamID & "' And " & _

"TeamTable.Description = '" & cOrigTeam.strTeamDescription & "' "
 
William,

Optimistic concurrency is based on testing an original value to a existing
value in the database.

If there is a difference in that, than you have a concurrency error. (There
is something changed meanwhile)

You can take a timestamp, or all the data that is used as the dataadapter in
versions 2002/2003 does it and standard in the 2005 version.

If you want to do that, than you should in one or the other way save
somewhere an/the original value of your rows.

I hope this helps,

Cor
 
It looks like you are using the saving-all-values approach to
optimistic concurrency.
If you are rollong your own approach to db concurrency (rather than
using dataadapter etc) then you'll probably need detect the conflict
and throw your own exception as well.

You can probably test the number of rows affect by the ExecuteNonQuery
to detect the conflict. Something like:

If adpTeam.UpdateCommand.ExecuteNonQuery() <= 0 Then
Throw New DBConcurrencyException()
'Or handle the concurrency conflict here without throwing exception.
End If

HTH
Andy
 
If I would use a timestamp, do I add the timestamp field to each table in
the Access database and manually update the timestamp at every update, or
does an mdb database file have a way to update a timestamp field each time a
row is changed?
 
Thanks, I'll try throwing that exception myself.

CaffieneRush said:
It looks like you are using the saving-all-values approach to
optimistic concurrency.
If you are rollong your own approach to db concurrency (rather than
using dataadapter etc) then you'll probably need detect the conflict
and throw your own exception as well.

You can probably test the number of rows affect by the ExecuteNonQuery
to detect the conflict. Something like:

If adpTeam.UpdateCommand.ExecuteNonQuery() <= 0 Then
Throw New DBConcurrencyException()
'Or handle the concurrency conflict here without throwing exception.
End If

HTH
Andy
 
William,

I am not sure about Jet (access), but the purpose of a timestamp is that it
is automaticly updated to the latest changetime. Why don't you just try it?

Cor
 
Back
Top