Help understanding database changes

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Hi, I don't have a code problem. I have an understanding problem.

This is a made up situation that mimics my real situation but it is
easier to understand.

1. I call a web method with an account number and retrieve an employee
object called employee on computer 1 (user 1). The employee object
getFirstName() returns "Michelle".

2. I repeat the process on computer 2 as above for the same account
number using the same web method (user 2). The employee object
getFirstName() returns "Michelle"

3. (user 1) I call employee.setFirstname("Mary") and then I invoke
employee.save(). The web method is called and the database for the
employee is saved.

4. (user 3) I perform a SELECT statement in the database and can see
that the table now contains 'Mary'.

5. (user 2) I call employee.setFirstname("Margaret") and then I invoke
employee.save(). The web method is called but an exception is raised.
When I query the database it still shows "Mary".

This is the behaviour I want but I do not know how it works. If I was
using a database where I had a connection for each user this behaviour
is easy to understand. But as I am using a web method and there is no
explicit connection to the database, and each user probably uses the
same database connection, I don't know how it is done.

I think it must work like this but is this right?

"Employee carries around an old copy with itself, and before doing an
update of the database, reads from the database, compares with the old
copy, and throw an exception if changed. "

The observed behaviour is what I want but would like clarification that
I am thinking how it is done is correct.

Thank you for when you help.
Mark
 
"Employee carries around an old copy with itself, and before doing an
update of the database, reads from the database, compares with the old
copy, and throw an exception if changed. "

There's your answer, in pseudocode. When a user fetches an employee record
from the database, the data in it is stored in an object. Add a parameter to
your WebMethod that takes the value that is on the client. Use that
parameter value to check the database. The client passes the currently-held
value on the client to the method. If it is the same as the record on the
server, it works. Otherwise not.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
I would guess that the client is caching a copy of what the data was
like before modification. When you call save() this copy is compared
with the database to test if any changes have been made by anyone else.

1) Does the client run a select command to fetch the original values
before commiting the data (thus the client performs the comparison)?

2) Does the client send to the web method both the original values and
the new values (thus the server performs the comparison)?


I've seen this when using SqlTableAdapters (especially if you have the
table adapter generate stored procedures for you).
 
Back
Top