wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Create method behavior clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Create method behavior clarification" Watch "Create method behavior clarification" New topic
Author

Create method behavior clarification

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2531
    
    8

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 OCPJP7 OCPWCD5 OCPBCD5
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5407
    
  13

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
Bartender

Joined: Sep 13, 2007
Posts: 2531
    
    8

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: 5407
    
  13

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
Bartender

Joined: Sep 13, 2007
Posts: 2531
    
    8

OK I get the idea. I'm using a singleton for my Data class so 1 instance always.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create method behavior clarification