I am doing FBN assignment, i thought i have completed my db package. But looking into some threads in this forum on RAF handle and synchronize methods, i started thinking i am not doing right. I have some questions, and need advice from you guys.
1. RAF handle:
In the given Data class, RandomAccessFile is instance variable. My each qui client will have unique Data object. In this case, suppose, if there are more than 300 clients, then there will be 300 file handles for open dbfile.
Would it not crash the server, when open file handles exceeds max no of allowable file handles for that OS ?.
If yes, To avoid this, do i need to make RandomAcessFile, a class level variable(i mean static RandomeAcessFile db) instead of instance variable ?
2. Synchronize methods and file I/O.
If one Data instance is shared among all clients then reading/writing to file is ok, since all methods are synchronized. Read will not be allowed unless write is completed or vice versa.
But if each client has unique Data object, in that case synchronize does not prevent one
thread to read, while other thread is doing write or vice versa concurrently.
case 1,
Thread T1 asks for read record 2,
at the same time T2 asks to modify record 2.
case 2,
Thread T1 asks for modify record 2,
at the same time T2 asks to modify record 2.
How will OS handle this situation(if i do not lock the record logically), will it allow both thread to read and modify concurrently(in case 1), / allow both threads to modify(case 2) same record concurrently ?
In assignment, while modifying i have to lock-->read-->modify-->unlock. Write is fine, but reader may get records that is being modified at the same time. How should i handle this situation. Since i am planning to have one unique Data object per gui client.
3. Since i am having one Data object per client, there is no need for synchronize method. But, if i remove "synchronize" from all the methods, my Data class is not threadsafe, if single instance of Data is shared among all clients.
Assignment instruction says "Because multiple concurrent connections may exist, you must make both your server and the suncertify.db classes threadsafe." Does it mean i must make my Data class thread safe, though i am providing unique Data object per GUI client ?
Please do not get angry, if my questions do not seem to reasonable.