Win a copy of The Java Performance Companion this week in the Performance forum!
    Bookmark Topic Watch Topic
  • New Topic

Run time problem

 
David Dong
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1033
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1033
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Report post to moderator
I don't know what you mean by extending the write properly.
Elaborately more please!

Thanks for your help
 
David Dong
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1033
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1033
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1033
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Report post to moderator
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 ]
 
David Dong
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 11888
203
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
    Bookmark Topic Watch Topic
  • New Topic