• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

NX: 5 problems about Data.java

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 All public methods is synchronized in my Data.java , should the private methods synchronized?
I think they shouldn't be synchronized, because if each public method is synchronized,then just one thread in the object,private methods is safe too.
2 The createRecord(String[] data) must throw DuplicateKeyException ,but which is the key ,i am confused ,about the contractor ,i can't see a key, now i just throw the DuplicateKeyException when matchs all the fields. is right?

3 The description of createRecord(String[] data) is "Creates a new record in the database (possibly reusing a deleted entry). " .my doubt that when reuse a deleted entry? must i reuse,can i never reuse?
4 May i add some methods in the interface DBAccess which the instruction provided? i feel if a getFieldInf() that get the information of Fields is more reasonable,or i must put the information of Fields in the "suncertify.properties ".
5 IOException and InterruptedException that i catch will rethrow a DataSystemException that extends RuntimeException,my doubt that is the DataSystemException should write in the comments doc,for example :
/**
* Reads the record from the database base on record number.
*
* @param long recNo - The number of the record to read (first record is 1).
* @return String[] - An array where each element is a value of record.
* @exception RecordNotFoundException - If a specified record does
* not exist or is marked as deleted in the database file.
* @exception DataSystemException - If database file could not
* be accessed with an I/O error occuring or if another thread
* has interrupted the current thread.
*/
public synchronized String[] readRecord(long recNo)
throws RecordNotFoundException ;
should write @exception DataSystemException ....?
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i
I'd appreciate any helps.
thanks!
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ZhiYuan

1 All public methods is synchronized in my Data.java , should the private methods synchronized?
I think they shouldn't be synchronized, because if each public method is synchronized,then just one thread in the object,private methods is safe too.


I agree with your second statement.

2 The createRecord(String[] data) must throw DuplicateKeyException ,but which is the key ,i am confused ,about the contractor ,i can't see a key, now i just throw the DuplicateKeyException when matchs all the fields. is right?


Doing a search in this forum for the word "DuplicateKeyException" for the last 5 days, brings up this thread. I know the heading says UrlyBird, but the same question applies to Contractors (as someone in that thread mentioned). Does this answer your question?

3 The description of createRecord(String[] data) is "Creates a new record in the database (possibly reusing a deleted entry). " .my doubt that when reuse a deleted entry? must i reuse,can i never reuse?


I think the use of the word "possibly" means that you do not have to reuse the deleted entries. But you can if you want to.
Personally I would. I want to build server applications that run 24 hours a day, seven days a week without maintenance. If you dont reuse the space, then a separate application would have to be developed to reclaim space. While this separate application is running, the server would have to be offline.

4 May i add some methods in the interface DBAccess which the instruction provided? i feel if a getFieldInf() that get the information of Fields is more reasonable,or i must put the information of Fields in the "suncertify.properties ".


I think the general consensus here is that you can add extra methods to the Data class if you want to, but you should not change the provided interface at all (do not add methods or change signatures of provided methods).
Regards, Andrew
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew,thank you for your reply!!!
about 1,3,4,i more clear.
i have see the topic which you tell me,

"Do what you want", but know what might be the consequences and to defend your decision in your design/choices document.
Mark


but i don't think that my " i just throw the DuplicateKeyException when matchs all the fields" is a good idea,and if i should add a key in the database ,such as useing both "name" and "location" be a key?
and what about the 5, should write ?
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ZhiYuan,

quote:
--------------------------------------------------------------------------------
1 All public methods is synchronized in my Data.java , should the private methods synchronized?
I think they shouldn't be synchronized, because if each public method is synchronized,then just one thread in the object,private methods is safe too.
--------------------------------------------------------------------------------
I agree with your second statement.


I agree with your second statement too. But maybe less with the first one. Do you mean that you synchronize the public methods as a whole ("synchronized" keyword in the method declarations) ? If you do, I think that you over-synchronize to the detriment of concurrency and that you contradict the DBAccess interface itself (because if you don't allow concurrent reads / updates / deletes, there is no need for the lock / unlock methods). Why not only synchonize the critical sections in your code ?

quote:
--------------------------------------------------------------------------------
3 The description of createRecord(String[] data) is "Creates a new record in the database (possibly reusing a deleted entry). " .my doubt that when reuse a deleted entry? must i reuse,can i never reuse?
--------------------------------------------------------------------------------
I think the use of the word "possibly" means that you do not have to reuse the deleted entries. But you can if you want to.


As a belgian french-speaker, I am not aware of the english semantic subtleties , but
I think the use of the word "possibly" means that you do have to reuse the deleted entries if it is possible to find one.

quote:
--------------------------------------------------------------------------------
4 May i add some methods in the interface DBAccess which the instruction provided? i feel if a getFieldInf() that get the information of Fields is more reasonable,or i must put the information of Fields in the "suncertify.properties ".
--------------------------------------------------------------------------------
I think the general consensus here is that you can add extra methods to the Data class if you want to, but you should not change the provided interface at all (do not add methods or change signatures of provided methods).


I agree with you Andrew. But I am still hesitating (see this pending thread about the best way to add those extra methods : directly in Data or by extending first the provided interface ? Your opinion would be welcome.
Regards,
Phil.
[ July 10, 2003: Message edited by: Philippe Maquet ]
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and what about the 5, should write ?


It seems perfect to me.
Phil.
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Philippe ,thanks

I think that you over-synchronize to the detriment of concurrency and that you contradict the DBAccess interface itself (because if you don't allow concurrent reads / updates / deletes, there is no need for the lock / unlock methods). Why not only synchonize the critical sections in your code ?


i agree with you ,i over-synchronized but for the thread safety ,i so for my safety, i synchronized all the public methods
i don't agree with you "there is no need for the lock/unlock methods" syschronized is for the safety of more than one threads accessing a Data object .and the lock/unlock is for the safety of more more than one threads which each have a Data object . so they are differently.
after your reply ,i doubt again,but i feel the word "possibly" is not your meaning . my english is very poor ,so i is not sure about it.
your say that thread i have seen, it is a good approach for add methods,but i can't use it just for your hesitating too.
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ZhiYuan,

I don't agree with you "there is no need for the lock/unlock methods" syschronized is for the safety of more than one threads accessing a Data object .and the lock/unlock is for the safety of more more than one threads which each have a Data object . so they are differently.


I am sorry to insist here, but I also understood those two levels of locking :
  • the "thread" or "network" level ("synchronized"-locking)
  • the "database" level (the lock/unlock methods)


  • But don't forget that both levels may interfere, and understand that - in your design - the second one will never have a chance to play.
    You may have 1000 of Data instances playing together, if you "whole-synchronize" all public methods, you'll never see your database-level locking mechanism enter the game, as you serialize all the calls (as in an indian file if you prefer).
    Please take some more time to think about it. In URLyBird 1.2.1, locking weights 80/400 points ...
    Cheers,
    Philippe.
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi ZhiYuan,
    I come back with a red face ...
    I was completely wrong here !
    I've been confused by the fact that my own design enforces only one instance of Data per database file.
    Synchronizing a method as in :

    is exactly the same as writing :

    So if you have one "this" object per client ... what you said is OK.
    But, BTW, is the "synchronized" keyword still needed then ?
    Sorry,
    a quite ashamed Phil.
     
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    [ZL]: 3 The description of createRecord(String[] data) is "Creates a new record in the database (possibly reusing a deleted entry). " .my doubt that when reuse a deleted entry? must i reuse,can i never reuse?
    [AW]: I think the use of the word "possibly" means that you do not have to reuse the deleted entries. But you can if you want to.
    [PM]: As a belgian french-speaker, I am not aware of the english semantic subtleties , but
    I think the use of the word "possibly" means that you do have to reuse the deleted entries if it is possible to find one.

    As a native English-speaker (of the American variety) I agree with Andrew. "Possibly" here is the same as "perhaps" or "maybe". If they had said "if possible" then I would agree with Philippe. But as it's written, you don't have to re-use deleted entries, even if it is possible. It's just an option.
     
    ZhiYuan Lin
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks
    hi philippe ,i misunderstanded , you are right if you use Singleton pattern to make only one Data.
    my Data can more than one ,and use lock()/unlock() to communicate each keep the database safety. of course it still hold some risks of dirty-data.
    hi
    a more question: About test the Data,how to simulate multi-thread to access Data objects, especially if useing junit is better,can someone help me?
    reply
      Bookmark Topic Watch Topic
    • New Topic