Hi everybody,
I developed a multiuser app which manages data stored on a MySql db. I
need to manage properly the following scenario of concurrent data
access:
1) user_1 opens a form to display a record data read from db 2) later
user_2 opens the same record and updates it 3) user_1 updates the record
Any database with decent transaction management should handle that
transparently, but there are two design issues:
1) be careful with auto-commit. You may be able to use it successfully,
but you'll have better control if you turn it off and use explicit
commits
2) If the user can read a record, look at it, scratch his head, get a
coffee and then make a change within the same transaction you *will*
get locking issues if the database is small. As the access key
population grows significantly bigger than the user population,
locking collisions become much less frequent, but their possibility
must not be ignored.
The best design will use two transactions:
- user reads data for display and immediately commits to release
read locks
- user wants to apply changes: when he hits 'enter' start an updating
transaction, re-read all rows, apply updates and commit without any
user interaction during this time.
This will work OK if the update merely makes additive changes to existing
rows, e.g., reduces a stock holding and inserts an associated invoice
line.
However, if the update /replaces/ values in an existing row you need
something better. Check whether the record is in use during the read
transaction and warn the user if it is. If not, mark the row(s) as 'in
use' and commit. Clear the 'in use marker' as part of the update
transaction and:
(a) make sure that you also unset it if the user decides not to
update anything
(b) design and implement an overnight batch process that clears
any left-over 'in use' markers as part of the daily backup.
Note that such 'in use' markers are application data. They are never
automatically managed by the DBMS and their management is non-trivial, so
if possible, design the database to avoid having to use them.