Win a copy of Microservices Testing (Live Project) this week in the Spring forum!

Abdel Kader

+ Follow
since Apr 14, 2007
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Abdel Kader

Agreed, but you cannot synchronize your methods using the RAF AND allow record locking. If you synchronize the RAF methods you basically lock all records since other threads are not allowed to execute on other records.
Mihai, my idea was not to open one RAF for every method. I wanted to open only one RAF for the entire db-file and use the same RAF for every method and then close it in finalize which is quaranteed to run once at least. The ide was to avoid opening and closing the file for every method. But since I discovered (and as you point out in your reply) that RAF is not threadsafe, this is no longer an option. Instead, now I create a new RAF object open and close the file in a finally-block for every method.

It is worth mentioning yet another time: RAF is NOT threadsafe. All access to the db-file must be synchronized or use an unique RAF object.
Yes, if all access is synchronized then it is threadsafe. However, if you wish to lock the db-file on a particular row like in the urlybird assignments, you will run into problem. For example it is known that the RAF.length is not thread safe and repositions the filepointer if you have multiple thread acceessing the file simultanously.

The reason why I started this topic is because I want to know if it is possible to use one single RAF and lock particular rows in a db-file rather than locking the entire file. This will fail if the RAF is not thread safe.
In that case im out of ideas :-(. Are you sure you get no exceptions at all?! In that case I wonder how it crashes? If the crash occurs before connecting any clients, your server should be more or less identical to the standalone before the crash. What exactly is it you are doing on that line where the server crashes? Try surronding it with try/catch(Throwable) and print any stacktrace...
Ok I will follow your example mate :-)

I found this at sun:

It's from 2003 but still "in progress" and there are other thread safe related complaints about RAF as well.. so better safe than sorry - open a new RAF for every read and write.

Since the assignment state that I may assume that only one program is accessing the file it is very very very tempting to open and close the file only once in order to improve performance. But i guess a few words in choices.txt will make them understand...
I have experienced a similar problem at work (not in my SCJD assignment). The problem was related to file-descriptors so I will have to ask you how you read the database file?
One single file object for all operations on the db-file?
Input streams, RAF?
Proper use of flush and close in finally?
Is it threadsafe? Try for example using only one client and see if the problem persists.
Is the crash random or does it always happen at the exact same line in your code?
Hmm, in general i would say that an exception is an "exception". Hence you should not use exceptions when the outcome is considered "normal". If a record does not already exist when creating a new one it is expected and therefore no exception should be thrown. In your case the method throws an exception either way which I would consider bad practice as there is no 'expected' behaviour.

Definitions from Sun:
"An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions."

"The Java programming language uses exceptions to handle errors and other exceptional events."

After all, the same people are going to grade that method of yours so I would recommend not to expect an exception.
I have used RandomAccessFile throughout my implementation of for the URLyBird but not without concern.

I dont want to open and close the file everytime I do read or write to the database so I create one and only one RAF object and keep the file open. The RAF is then closed in my overriding finialize method. Since the RAF was created using the "rwd" parameter is writes to disk continously and keeps the file up to date with the object.

Accoring to some forums and bug-reports the RAF is not entirely thread safe, if thread safe at all... Nothing is written in the API so I simply ask anyone with the knowledge.

Too what extent is RAF Threadsafe? Or is my approach completely wrong?
Just some clarification (from anyone who might know)...

I may use anything from the javax.swing and java.awt libraries?
If not, please explain which are not allowed.
Ok, sorry for the name-issue, hope this is better. Thanks for the quick reply - I will start to investigate search buttons at this site right away
Hi everyone,

Im new at this ranch and have some problems understanding the assignment for URLyBird regarding the networking and database specifications. I have been reading the assignment a couple of times but there is one thing I simply just don�t understand. However, there are a lot of "must" in theese sentences and better safe than sorry so... Please help :-)

It is clear that the application must be started from the command line using only 3 modes (server, alone or <empty> . This is how I have understood the different mods - please let me know if im wrong.

When launching "standalone" the "normal" client GUI should appear but not connected to a database on a server. Instead the application should use a local database and hence the user must specify where the local database is by selecting a .db file on the filesystem.

When launching the application clean of command line arguments the "normal" cilent starts where the user must supply the port/ip/name of the server to connect to.

When launching "server" a simple gui will be displayed where the user may choose where the database file is. However, in the assigment (in the chapter describing the server) it clearly says that the user MUST be able to choose a non-local or local database. It also says that if the user choose a local database, the networking MUST be disabled. As I understand it the server is completely useless without networking since no-one will ever be using it (no networking = no requests). Further I do not understand the need for anything else than a local database on the server. Should you (in the server application) be able to point out a non-local database on say another machine?