| Author |
Create method behavior clarification
|
K. Tsang
Ranch Hand
Joined: Sep 13, 2007
Posts: 1259
|
|
Hi all, I have a simple question about my create method. Suppose I use a set to keep track of locked records. At any one time, this set should have only one element from the lock method.
Now since we don't need to call lock before create and create doesn't take an int, I'm able to still add the newly generated record number to the set if the set already had an element.
If this behavior is true, there are 2 possible threads modifying the database file. For example, 1 thread create and the other update/delete.
Am I correct?
|
K. Tsang JavaRanch SCJP5 SCJD/OCM-JD
|
 |
Roel De Nijs
Bartender
Joined: Jul 19, 2004
Posts: 4389
|
|
and thread 3 is maybe reading a record, and thread 4 does a findByCriteria. all crud-methods are changing the file pointer, so you have to make sure moving the file pointer and reading or writing a record is executed as one atomic operation.
This has nothing to do with lock/unlock a record.
So you need protection:
- at file-level: make sure your file-pointer isn't moved before you actually can read/write, because that will corrupt your db-file
- at record-level: a record can only be deteleted / updated if it's locked
you have to implement both protections because each one is focussing on a different issue
|
SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
|
 |
K. Tsang
Ranch Hand
Joined: Sep 13, 2007
Posts: 1259
|
|
|
Thanks Roel. If I don't protect either the file or record, when I run my multithreading tests it should deadlock right? Just to make sure.
|
 |
Roel De Nijs
Bartender
Joined: Jul 19, 2004
Posts: 4389
|
|
|
i don't know if it deadlocks, but what i know is when you don't do both, you could end up with a messed up db-file (if you work with 1 single RAF and multiple data-instances) and rooms begin booked by several different customers
|
 |
K. Tsang
Ranch Hand
Joined: Sep 13, 2007
Posts: 1259
|
|
|
OK I get the idea. I'm using a singleton for my Data class so 1 instance always.
|
 |
 |
|
|
subject: Create method behavior clarification
|
|
|