|
Sebastien, if you search the list archives you'll find
tons of references into this subject. Here's a summary of the most common
solutions:
1) Ask
the container to serialize transactions (TX_SERIALIZABLE). It may depend on the
DB(some containers just delegate the isolation on the DB).
2) If
using JDBC directly, and the DB supports it, lock records in the SELECT
statement. Oracle code:
SELECT
FOR UPDATE * FROM TEST
Be
careful. All DBs that support this will aquire a write lock on the involved
records at best, but will fall back to a page and even the whole table if they
don't have enough resources to acquire and maintain a more fined-grained lock.
Only DB I know of that actually locks single records is Oracle, which implements
a field versioning algorithm. This is a better performing solution than
serializing access altough its not a standard RDBMS feature and the gains in
performance depend on the resources/load on the RDBMS
server(s).
3) Implement your own Versioning Algorithm
to solve races. Works like this:
-
Every table that supports versioning as an extra field named version(or other, I
used "vvvversion" in my implementations).
- The
isolation level must be READ_UNCOMMITTED(dirty reads)
- When
you select a record you also get the version field.
- You
ensure that an update does not ocurr if the version field has been touched and
that the update operation changes the version field(to whatever else, a sequence
is nice).
1 -
SELECT ID,NAME,VERSION FROM NAMES WHERE ID = 1
Result
is (1, 'JPL', 23432)
2 -
UPDATE NAMES SET NAME='Juan Pablo Lorandi', VERSION=19192 WHERE ID=1 AND
VERSION=23432
To
apply to your example:
User A
User B
1.
SELECT * FROM TEST
2.
SELECT * FROM TEST
3.
UPDATE TEST SET .... WHERE TEST_ID = n AND
VERSION=v //Success!!!, Update count = 1
4. UPDATE TEST SET ..... WHERE TEST_ID = n AND VERSION=v // Silent failure, Update count =
0
The big difference between versioning and serializing
access is that the first client to issue the update wins the race in versioning,
whilst the first to execute the select wins in serializing. Usually versioning
performs better, whilst serialization provides a more business logic friendly
approach to isolation(maintaining ACID rules).
HTH,
Juan Pablo Lorandi
Chief Software
Architect
Code Foundry Ltd.
Barberstown, Straffan, Co. Kildare, Ireland. Tel: +353-1-6012050 Fax: +353-1-6012051 Mobile: +353-86-2157900 www.codefoundry.com Disclaimer:
Opinions expressed are entirely
personal and bear no relevance to opinions held by my employer.
Code Foundry Ltd.'s opinion is that I
should get back to work.
|
Title: Message
- Concurrent record update BOUTTE Sebastien
- Re: Concurrent record update Ashwani Kalra
- Re: Concurrent record update Juan Pablo Lorandi
- Re: Concurrent record update Sanjeev Verma
- Re: Concurrent record update Juan Pablo Lorandi
- Re: Concurrent record update Ramakrishna N
- Re: Concurrent record update Juan Pablo Lorandi
- Re: Concurrent record update Juan Pablo Lorandi
- Re: Concurrent record update BOUTTE Sebastien
