I have some problem in my data class .About the I/O operation I want to create a RandomAccessFile object and all method use it by synchronization.On the server side only have one instance of the data class.
for exmple :
I don't think this an good approach.Could you present a new one?. BTW how to align the word! thanks a lot
There is nothing particularly wrong with the aproach you have displayed - at least so far. Is there some other design decision you are thinking about that you think may cause problems with this design? Or could you explain where you are having doubts?
BTW how to align the word!
Are you asking about what standard to follow? If so, I recommend the Sun Code Conventions for the Java Programming Language.
Hengxu's implementation of Data class is similar to mine except that I have many instances of Data in the server side to identify different clients and I make the Data class be a Facade.
I want to know in hengxu's Data class where I can add "raf.close()"? I think adding this in either methods of Data class cannot work. But without it, there isn't safe enough. What should I do?
Another question. You taught me that I should ensure that clients only call my Data class and change my access level of the classes bihind the Facade to restrict access. Here is a class named Contractor in the database package. The instance of DataAccess class which is behind the Facade reads data from the db file to an array made up of Contractor's object. I want to ask whether the classes in GUI package should be able to access the Contractor class? I think it should be, because within the MyTableModel class in the GUI package, many methods need the instance of Contractor such as getValueAt/setValueAt, is it right? And it seems as if it doesn't match your advices,I'm not sure.
Thanks in advance! Regards,Ailsa Cape [ August 19, 2005: Message edited by: Ailsa Cape ]
Hi Andrew,Alisa Thank you for your reply. I'm a new rancher and i'm in China.so if my word is unreadable.I must say sorry. Alisa ,i'm interested in your approach. but I have a question. why need many instances of Data in the server side ?
Is there some other design decision you are thinking about that you think may cause problems with this design?
my idea is on the server only have one instances of Data and in each method have a new RAF,the methods have nothing synchronization code. then the code is reentrant. the Thread safety only be guaranteed by the lockmanager. so if one client want to do any operation about the database must lock the record.
like this:
if a client want to read the record
is it ok? another question :if in my lockmanager I use hashtable .is it necessary synchronization. i don't think so except cookie generator.(just me) [ August 19, 2005: Message edited by: hengxu sun ]
I'm a new one as well, and I asking myself questions about Data class...
I have chosen to implement a data cache between physical data (the file) and the server.
The Data class will use the cache to read/update data. Cache map a record with its record number. The cache will be loaded at startup.
With basic lock consideration the processus is the following: When reading, it will get object reference in cache. When updating, it will: get object reference in cache, lock access to this data, update the cache, update the db file, unlock access to this data.
I have read in others posts that cache approach is also correct. Whats the traps of such an architecture ? Should I better develop something simplier like hengxu suggest ?
Like Alisa I'm asking myself if it's better to keep the file open while server is active, or if it's better to open/close file on each operation on it ?
Also, is there is a way to ensure the file is not corrupted from outside ? What happen if another server is launch (say with a different port number) but using the same file... how avoid mess and inconsistency ???
I prefer RMI to socket for the remote service. So I need each client has its own instance of Data to identify each other. Thus, the lock method in class LockManager can lock a specific record via lock(recNo, Data data).
I want to know in hengxu's Data class where I can add "raf.close()"? I think adding this in either methods of Data class cannot work. But without it, there isn't safe enough. What should I do?
You must remember that you have a different assignment than Hengxu, so your solution to this problem could easily be different than Hengxu's. For example, since you are using a facade, you may not need multiple instances of the RandomAccessFile.
Some ideas for you:
Henxu suggested opening/closing the data file for each database operation. Personally I think that this is would cause high overhead, however it might be workable with a cached system.
You could create an additional interface that your Data class can implement, which will define a method to close the database - your server can verify that Data is an instance of this class and, if so, use the defined method to close the database.
As a last resort you could use the finalize() method to close the database.
Another question. You taught me that I should ensure that clients only call my Data class and change my access level of the classes bihind the Facade to restrict access. Here is a class named Contractor in the database package. The instance of DataAccess class which is behind the Facade reads data from the db file to an array made up of Contractor's object. I want to ask whether the classes in GUI package should be able to access the Contractor class? I think it should be, because within the MyTableModel class in the GUI package, many methods need the instance of Contractor such as getValueAt/setValueAt, is it right? And it seems as if it doesn't match your advices,I'm not sure.
Sorry if I wasn't clear here. Any classes your Data class calls should be hidden. However your Data class has no knowledge of the Contractor's object (your Data class should be returning an array of int's from the find object, and so on).
Any classes you choose to add in front of the Data class (for example: to give you a find method that returns an array of Contractor records) would presumably need public access, as would the Contractor class itself.
Alisa ,i'm interested in your approach. but I have a question. why need many instances of Data in the server side ?
There are many variations on the assignments. One of the more common variations discussed here is the presence (or absence) of cookies returned from the lock method and used in the unlock, modify and delete methods. Your assignment has lock cookies, Ailsa's does not. Without the lock cookies, it is a little more challenging to ensure that calling the lock method "locks a record so that it can only be updated or deleted by this client" - how do you identify the client? If you are using sockets then you can use the thread ID, however this is not valid for RMI. One solution to this is to use multiple instances of the Data class, possibly hiding the real work in classes behind a facade.
if a client want to read the recordis it ok?
What do you obtain a lock cookie here?
another question :if in my lockmanager I use hashtable .is it necessary synchronization. i don't think so except cookie generator.(just me)
I don't think the synchronization is needed even in your cookie generator. But that could be just me . Why do you think it might be necessary to have the internal methods of your collection synchronized?
Sorry for just asking questions, but it is better for you to try and find the answers than just reading my opinions.
I have read in others posts that cache approach is also correct.
Just being pendantic here, but using a cache can be a valid design choice, however it is not the only design that is correct .
Whats the traps of such an architecture ?
Potentially you are writing code that is not mandated by the instructions. This gives you code that you cannot get extra marks for, but you could loose marks for if you get it wrong.
Should I better develop something simplier like hengxu suggest ?
This is something you have to decide - is the performance gain (and it can be a big gain, especially when doing searches) worth the extra code? How much extra code is involved? Is the extra code going to make it easier or harder for a junior programmer (assignment assessor) to understand?
There is no one right answer. What is important is that you think about the questions, and decide what is right for you. And then be prepared to document your decision in your choices document, and possibly defend your decision in your essay exam.
Also, is there is a way to ensure the file is not corrupted from outside ? What happen if another server is launch (say with a different port number) but using the same file... how avoid mess and inconsistency ???
Do you have a line in your instructions similar to: "You may assume that at any moment, at most one program is accessing the database file"? If so, you do not have to worry about this.
Thank you again for your instructive advices. Recently, I benefit from a lot of your posts. They show your rich experience and talent for Java program designing. Best wishes for you! I decided to follow your advice to make the class Contractor public access.
I find my solution is more complicated and don't know how to predigest it. From an OO perspective, each class should have only a single responsibility, so I designed a class named SchmaHeader in the database package to read the db file's header information such as fieldNames etc.
Because DataAccess class which is behind the Facade(the Data class) uses the instance of SchmaHeader, I try to restrict the access of the methods in SchmaHeader then they cannot be accessed by any class in any other package. I found a bit more complex to achieve this since DataAccess,Data,LocalData and DataInterface must all have the same method signature as "getFieldNames" method in SchmaHeader class. Although I have done this, I still think there may be a simpler way.
I want first to thanks Andrew, who provide clear answer that help me to get my head not too much confuse with this certification.
I have read in others posts that cache approach is also correct. Whats the traps of such an architecture ? Should I better develop something simplier like hengxu suggest ?
There is no one right answer. What is important is that you think about the questions, and decide what is right for you. And then be prepared to document your decision in your choices document, and possibly defend your decision in your essay exam.
I finally chose to not implement the cache as it is not a 'must' and a source of trouble. I let the door open to subclass my data access class (the one used by the Data class)to provide a cache system.
Then I avoid to add problem yet and the cache system is still an option for future.
Post by:autobot
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
a bit of art, as a gift, that will fit in a stocking