• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create method behavior clarification

 
K. Tsang
Bartender
Posts: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Roel De Nijs
Sheriff
Posts: 10591
140
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
K. Tsang
Bartender
Posts: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10591
140
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3521
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I get the idea. I'm using a singleton for my Data class so 1 instance always.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic