File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Run time problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Reply locked New topic
Author

Run time problem

David Dong
Ranch Hand

Joined: Aug 15, 2004
Posts: 58
I wrote the below code. It compiled but when I run it, my main.getRecordCount() is not updated and it should
because the main.create() creates a new row to the database.
Here is my code:

String[] a={VALID_RECORD+"","Dogs with To ","Whoville","aa",null,null,"bb"};
int n=main.create(a);
System.out.println("created row "+n);

for(int r=1;r<main.getRecordCount();r++){
String[] str=main.read(r);
StringBuffer rv = new StringBuffer("recordNumber = " + r +"\n");
for (int i = 0; i < main.fieldInfo.length; i++) {
rv.append( main.fieldInfo[i].getName()+" "+" = " + str[i]+"\n");
}
System.out.println(rv.toString());
}

Question is: How do I make sure the once the database gets updated and main.getRecordCount will get the updated record count?

I hope this is not too confusing for you helper..
anyhelp will be appreciated,
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by David Dong:
I wrote the below code. It compiled but when I run it, my main.getRecordCount() is not updated and it should
because the main.create() creates a new row to the database.
Here is my code:

String[] a={VALID_RECORD+"","Dogs with To ","Whoville","aa",null,null,"bb"};
int n=main.create(a);
System.out.println("created row "+n);

for(int r=1;r<main.getRecordCount();r++){
String[] str=main.read(r);
StringBuffer rv = new StringBuffer("recordNumber = " + r +"\n");
for (int i = 0; i < main.fieldInfo.length; i++) {
rv.append( main.fieldInfo[i].getName()+" "+" = " + str[i]+"\n");
}
System.out.println(rv.toString());
}

Question is: How do I make sure the once the database gets updated and main.getRecordCount will get the updated record count?

I hope this is not too confusing for you helper..
anyhelp will be appreciated,


How are you writing the file? If you are using a RandomAccessFile the write should extend it properly. If you are using a Stream you will need to use a flush() to force the data out.
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Please do NOT multipost, its quite annoying to answer a post only to find another from the same OP with the problem already solved!

On some less friendly fora (eg. forums.java.sun.com) you would get seriously flamed for this.
David Dong
Ranch Hand

Joined: Aug 15, 2004
Posts: 58
I don't know what you mean by extending the write properly.
Elaborately more please!

Thanks for your help
David Dong
Ranch Hand

Joined: Aug 15, 2004
Posts: 58
I am sorry. I thought a different topic can attract more helper like you.
So I tried two different topic. I make sure I won't do that again.
;p
David Dong
Ranch Hand

Joined: Aug 15, 2004
Posts: 58
The problem has not been solved. I have create a row before calling getRecordCount but getRecordCount only returns the old count instead of new updated..

Please help!
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by David Dong:
The problem has not been solved. I have create a row before calling getRecordCount but getRecordCount only returns the old count instead of new updated..

Please help!


Sorry, I just saw three replies and an ongoing discussion, so I assumed you were getting assistance.

I see from your other post that you are using a RandomAccessFile. Do bothe the read code and the update code use the same RAF? Do you have many instances of the RAF or just one. If you have multiple instances, you should only have one. I make my Data class use a Singleton DatabaseFile class that does all the low level I/O.
David Dong
Ranch Hand

Joined: Aug 15, 2004
Posts: 58
It's okay. I am not the type of person who get mad easily but less calm.

Do bothe the read code and the update code use the same RAF?
yes
I think I am using singleton database because I did not open multiple connection to the same database.
[ September 12, 2004: Message edited by: David Dong ]
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by David Dong:
It's okay. I am not the type of person who get mad easily but less calm.

Do bothe the read code and the update code use the same RAF?
yes
I think I am using singleton database because I did not open multiple connection to the same database.

[ September 12, 2004: Message edited by: David Dong ]


That's ok, it's gettin late here, but I'll do some testing on this in the morning. At present I don't actually use the create and delete actions.
David Dong
Ranch Hand

Joined: Aug 15, 2004
Posts: 58
I think it might be the for loop, since create takes a while to finish and getRecordCount started before finished creating a row.
but I don't have any thing to support this theory.


You help alot peter.
Thank you,

Who can solve this mystery?
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by David Dong:
I think it might be the for loop, since create takes a while to finish and getRecordCount started before finished creating a row.
but I don't have any thing to support this theory.


You help alot peter.
Thank you,

Who can solve this mystery?


I think you have solved the mystery, if the getRecordCount isn't synchronized.

I enabled create in my code and the record number is advanced properly. I calculate it using

This calculation is done after the write, while still synchronized on the RAF. I then store it in a instance variable so that other threads can see it using a simple getFileSize call. If you do that calculation without being synchronized on the RAF the raf.length() may return incorrect results if an append is in progress.
Mike Vess
Ranch Hand

Joined: Aug 25, 2004
Posts: 41
Originally posted by David Dong:
I think it might be the for loop, since create takes a while to finish and getRecordCount started before finished creating a row.


What do you mean by saying that getRecordCount is started before finished creating a row? The creation of a record is done after this line "int n=main.create(a);" and then your database file should be updated.

Then in the for loop "for(int r=1;r<main.getRecordCount();r++){" the getRecordCount should be updated with a new value but this loop will always fail if any record other than the last in your db is deleted since you call "String[] str=main.read(r);" on every record which should throw a RecordNotFoundException if record not found.
[ September 13, 2004: Message edited by: Mike Vess ]

What you thought was right today may need a refactoring tomorrow...
David Dong
Ranch Hand

Joined: Aug 15, 2004
Posts: 58
Peter: Thanks for your help. It really means a lot to me. But I still could not solve the mystery. My getRecordCount method still get the old count for a reason.

Mike: Thanks for your kind suggestion but I want to solve this mystery. Why is my getRecordCount method gets the old count even it is call after I called my synchronized create method.
David Dong
Ranch Hand

Joined: Aug 15, 2004
Posts: 58
Let me first thank you Peter and Mike for helping. I had mistakenly make the for loop error. Hope I don't make that mistake again.

Thanks again..
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi David,

I have closed this topic, as it is a duplicate of this topic. I decided to close this topic as the other topic has more of your source code.

You should only ever post your question once on any given site.

If you feel that people in other forums may have ideas which can help you, you might want to post a message in the other forum saying something along the lines of "I am looking for help on topic 'xxx' and I have asked it in thread <url>".

If you are afraid that no one has seen your question, then after at least 24 hours you might want to post a reply to your own message asking if anyone has any ideas.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Run time problem
 
Similar Threads
Block Sliding Puzzle
Line Breaks when displaying HTML
getRecordCount
JPEG Image Upload
reading a text file from a JSP page