Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: UrlyBird database design question

 
Chee-Chan Keng
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would like to know if my following design in thread safe:
1. I make my Database class a singleton, only 1 Database object exist throughout the program. (constructors are private)
2. I make all accessible Database methods synchronized, is this the right way to let only 1 thread access Database at a time?
3. I use ArrayList to hold my hotel lists, since I assume only 1 thread is accessing my Database at a time.
Is the design thread safe using ArrayList instead of Vector? 'coz I think Vector is redundant with 1 thread accessing the Database object at a time.
thanks a lot!
 
Satish Avadhanam
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keng
Originally posted by Chee-Chan Keng:
Hi,
I would like to know if my following design in thread safe:
1. I make my Database class a singleton, only 1 Database object exist throughout the program. (constructors are private)

This is OK.

2. I make all accessible Database methods synchronized, is this the right way to let only 1 thread access Database at a time?

I have a question here. When the Database class is singleton, then however only one instance of the class is accessing the database right. Then why would we need synchronization which prevents two instances changing a shared variable? Probably I did'nt understand the concept or I don't know.

3. I use ArrayList to hold my hotel lists, since I assume only 1 thread is accessing my Database at a time.

I think, its ok.

Is the design thread safe using ArrayList instead of Vector? 'coz I think Vector is redundant with 1 thread accessing the Database object at a time.
thanks a lot!

Good Luck
 
Chee-Chan Keng
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding question 2, when the database is singleton, I have 1 instance of class accessing the database, but I might have 2 threads calling Database.deleteRecord() at the same time, so I need to synchronize the methods so the 2 threads dont' call deleteRecrod() at the same time, is my understanding correct? I never tested how synchronize actually behave, only read the "theory" from book
 
Satish Avadhanam
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keng
Originally posted by Chee-Chan Keng:
Regarding question 2, when the database is singleton, I have 1 instance of class accessing the database, but I might have 2 threads calling Database.deleteRecord() at the same time, so I need to synchronize the methods so the 2 threads dont' call deleteRecrod() at the same time, is my understanding correct? I never tested how synchronize actually behave, only read the "theory" from book

Yeah maybe you're right. I too am not comfortable with synchronized code. You can wait on for other's opinion on this. Sorry that I cannot help
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe your Database class implements the Sun's DB or DBAccess interface.
Adding the modifier synchronized to the methods, wouldn't change the signatures, but still I wouldn't feel comfortable doing that.....
If you provide explicit locking to prevent simultaneous updates to the same record and allow only 1 thread to read/write a record to the database file at a time, then maybe you can get away without syncronizing all the methods in the DB interface?
 
Chee-Chan Keng
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vish,
Here's my class structure:
- interface is DB.java
- SUN's required class is Data.java
- my Database.java is accessed by multiple threads from Data. So methods in Data are not synchronized, but methods in Database is.
What do you mean by explicit locking? can you give an example of explicit locking?
thanks!
:roll: <-- I like these icons -->
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chee-Chan Keng:
What do you mean by explicit locking? can you give an example of explicit locking?

Chee-Chan,
Sorry, I thought you are talking about the Data.java class.
I just meant......
lock()....
perform_action_like_update_or_delete()....
unlock()....
So, instead of synchronizing the complete method, you could syncronize parts of it. Well it depends on your design if your complete method in the method needs to be synchronized, then you can syncronize the complete method...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic