• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLyBird 1.3.1 DB Access layer design, pls rewiew again

 
Leo Tien
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, this is a old topic, several thread have discussed on it again and again, my design is come from these threads too. But I have several question and confusion on my design, and beg someone's help. At first, I want to say --Thanks !
Here's my ugly design:
1. [Data] Alone my assignment, I have a DBMain interface and a class named Data implement this interface. In the Data class, there is a static attribute WeakHashMap, store locked records information. I want use multiton Data design pattern, every client have a Data instance. So like others I use 'this' as a cookie that describe who lock the record. In this scheme, I have a stupid idea. Why I use WeakHashMap, because it should remove a item from it when its key is no longer in ordinary use. So I think whether I can use a thread and HashMap replace this. Every time a user lock a record, I can put 'this':'recordNo+currentTime' into the HashMap, and run one thread check this HashMap's item looply, the thread remove one item from the HashMap as soon as it find this item is timeout(the time is define already). This idea correct ?
2. [DataSchma] This class contains come metadata of the database such as MAGIC_COOKIE, fields name, fields length and so on. And contains a RandomAccessFile as its static attribute, this RAF is private, DataSchma supports getRAF() method return it. DataSchema is singleton, it as a static attribute in Data. Data execute it's getRAF() method to get a RandomAccessFile instance, and use it to access Db file. My question is: because the RAF is a DataSchema's attribute, and MAGIC_COOKIE is read from DataSchema, whether this against sun's assignment -- "Your data access class must be called 'Data.java'," ?
3. [DataHelper] I find many people implement it. I don't know whether use it and what's its advantage ?
4. [DataAdapter] It has only find and update function that the assignment requires. It contains Data instance as its attribute. In other thread I found some people implement Data class that set all methods sysnchronized. But because every client have a Data instance, this is needed ? Whether I can do this sysnchronized mechanism in DataAdapter ? Because the dead lock is exist, where it can take place in my design ? Give me some advice, pls !
Thank for read my lengthiness text .
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leo,
  • Check your instructions - most have a requirement that "If the specified record is already locked by a different client, the current thread gives up the CPU and consumes no CPU cycles until the record is unlocked." - this is pretty specific about what Sun want: timeouts don't meet that criteria.
    If you read Pep Menval's topic "locking a record", you will see that implementing timeouts instead of blocking is a really bad idea for this assignment.
  • What Sun require (IMHO) is a class called Data which will allow your other classes to access the database. In effect it is a fa�ade for the database.
    So if your server only calls methods in the Data class (but your instance of Data class happens to call methods in other classes) then you should be fine.
    But if your server calls DataSchema class directly, then you will be breaking the instructions (IMHO).
  • As mentioned in the previous point, you may have more than one class in your "Database" section, as long as all these other classes are hidden behind the Data fa�ade.
    This has the major benefit of enabling you to limit the responsibilities of the various classes. Instead of having one class which is responsible for "file manipulation" and "record locking" and "searching" and "...", you could have separate classes for each of these functions - each class then becomes responsible for only one thing which makes development and maintenance easier.
    Also, in the case where you are using the instance of your Data class as the client identifier (as opposed to those candidates who got instructions requiring them to use a "cookie"), you have to have an instance of the Data class for each connected client. You probably don't want each instance of the Data class to have it's own file handle, so it can make sense to delegate all file operations to a separate helper class.
  • Is DataAdapter the class that you are exposing to the client? Have you looked at the topic "Should lock methods be callable by the client"?
    You will need some synchronization in your low level file access methods. Consider what happens when you write the update to file: normally you want to seek() to the correct position in the file, then write the data. If you do not have synchronization, one thread could seek to the start of record 5, then another thread could seek to the start of record 10, then the first thread could write it's update (overwriting record 10 instead of 5), and then the second thread could write it's update (overwriting record 11 instead of 6). Actually that is a simple example of what could go wrong - there are far more "interesting" ways that not having data access synchronized could corrupt your data.


  • Regards, Andrew
     
    Leo Tien
    Ranch Hand
    Posts: 156
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Andrew, thanks for your reply:
    What Sun require (IMHO) is a class called Data which will allow your other classes to access the database. In effect it is a fa�ade for the database. ..................As mentioned in the previous point, you may have more than one class in your "Database" section, as long as all these other classes are hidden behind the Data fa�ade.

    Whether you mean that the Data class is face to network layer, and not to db.db file directly ? As follow:
    server(network layer) -- Data ---- other db access classes -- db.db
    You probably don't want each instance of the Data class to have it's own file handle, so it can make sense to delegate all file operations to a separate helper class.

    Now I don't fully understand your mean, I will serach and study 'DataHelper' class, then ask you some question on it.
    Is DataAdapter the class that you are exposing to the client? Have you looked at the topic "Should lock methods be callable by the client"?

    Before post this thread, I will expose the DataAdapter to the client, more exactly to the server. ( server -- DataAdapter -- Data )
    Now, I know this is wrong. Whether I should support this class in server layer, not in data access layer. ( client GUI -- DataAdapter -- server ) ?
    My english is poor, do you understand me? If not, please point it out, I will statement it more detail again.
    Thanks for answer my stupid questions.
    Thanks for tire your eyes.
    [ November 02, 2003: Message edited by: Leo Tien ]
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 12014
    220
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Leo,
    Originally posted by Leo Tien:
    Whether you mean that the Data class is face to network layer, and not to db.db file directly ? As follow:
    server(network layer) -- Data ---- other db access classes -- db.db

    I am saying that you can do that, and I gave some examples of why you may want to do that. This was in response to your question on what is the advantage of a DataHelper class.
    You don't have to do this - it is up to you to determine whether it makes sense for your application.
    Originally posted by Leo Tien:
    Before post this thread, I will expose the DataAdapter to the client, more exactly to the server. ( server -- DataAdapter -- Data )
    Now, I know this is wrong. Whether I should support this class in server layer, not in data access layer. ( client GUI -- DataAdapter -- server ) ?

    Perhaps you should explore why you believe your original concept is wrong. In determining what your reasons are, you may find an answer to your new question
    Regards, Andrew
     
    Philippe Maquet
    Bartender
    Posts: 1872
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Andrew and Leo,
    I am just coming in about timeout.
    It looks like you just used the same word for two different concepts.
    Leo's timeout concept is "after-lock" hence "after-wait", while Andrew's one is "while-claiming-for-a-lock" or "while-waiting". Of course, this said, both of you are right.
    Best,
    Phil.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic