Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Opening and closing the file

 
Raymond Tanner
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use a RAF to read in the records and I have a get method to get
a handle to the RAF. I currently keep the file (the RAF) open at all times and rely on the system exit to close the file. Is this bad?
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO, it is generally bad practice to rely on this. If your application or JVM crashes for whatever reason, the file may be corrupted.
 
Raymond Tanner
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you. but isn't a lot of over head to keep opening and closing
my RAF?
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't if you cache the data. You only need to reopen it when you update it.
 
Raymond Tanner
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken,
yeah, thats my problem. I am not caching the data. I am going
back and forth on this. I was finished testing but realized a few
possible holes. See my other recent post if you will. I saw in other posts
both andrew and mark saying caching is not necessary but if you go
with a singleton I am thinking otherwise.
ray
[ January 10, 2004: Message edited by: Raymond Tanner ]
 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not caching either and was wondering where and how I should close my RAF. Should I have a method in my Data class that closes the RAF and then call this method when shutting down the server, and when closing the client in standalone mode?
 
Raymond Tanner
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats what I am hoping someone will help us out on.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe & Raymond,
I agree with Joe's suggestion - have a method which will allow you to explicitly close the database, and call it when you exit.
Regards, Andrew
 
Xie Ruchang
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
This is what I have done, to share with the rest and also introduce another question.
I include in Data.class a method called close(). Data in turn call DataFile.class which has an instance of raf and close the raf using raf.close().
My Data.class is the local version. My DataProxy.class is the network version. Both classes has the close() method, one closes the file and the other closes the network connection. Both classes implement DBACCESS as this is compulsory, stipulated in the spec.
My Business class has an instance of Data or DataProxy depending on whether it is network or standalone. The instance is of type DBACCESS.
Now the problem is DBACCESS interface does not have a close() method and we CANNOT change the interface. How to call the close() method to release the resources?
Best Regards
 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whynot just call the close() from your Data class. just make it static.
Data.close()
 
Xie Ruchang
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,
How do you distinguish which to call since the client only know that it is a DBAccess and not Data or DataProxy.
It could be
Data.close() or DataProxy().
Further, static methods cannot access member variables like the raf in DataFile.
Best Regards
 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my raf is static as well so it is accessible to the static method. and my business methods are server side. so the client gets a DBAdapter. when i shutdown my rmi server, i call Data.closeDBFile() to close the database. in standalone mode i make the same call, but when exiting the client.
 
Peter Yunguang Qiu
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't if you cache the data. You only need to reopen it when you update it.

Do you mean that whenever you write/update a record, you open the physical file and then close it?
 
Xie Ruchang
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Questions for Joe

my raf is static as well so it is accessible to the static method.

Where are the actual filename and raf stored if it is static. You must have an instance of raf before you can raf.close()?
It seems to me that you are using C kind of programming style on a object-oriented language like Java. This is not OO and you may have problems in Object-oriented design part worth 30 points. Perhaps I am wrong. Please enlighten me.
As far as I know, the parts that are static should be the utility methods and the public static void main for instantiate the objects.
 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frankie Cha:
Questions for Joe

Where are the actual filename and raf stored if it is static. You must have an instance of raf before you can raf.close()?
It seems to me that you are using C kind of programming style on a object-oriented language like Java. This is not OO and you may have problems in Object-oriented design part worth 30 points. Perhaps I am wrong. Please enlighten me.
As far as I know, the parts that are static should be the utility methods and the public static void main for instantiate the objects.


i have an instance variable raf which is static in my Data class. all code that changes the file pointer are synchronized on raf. filename is passed into the constructor. i made the closeDBFile() method static since all it does is close the databse file everytime and so that both the client(standalone) and server could close the database file upon exiting. Data.closeDBFile() seemed like the easiest way to close the file.
[ January 10, 2004: Message edited by: Joe Black ]
 
Raymond Tanner
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you server is running and you use the Ctrl command to stop the server,
how possibly are you going to close your file via a command? You cant? Correct?
 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this was discussed in a recent thread. i use a server gui. much easier.
 
Vrinda Werdel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
IMHO, it would help to include a shutdown hook to ensure that file handle is closed before the process is terminated by pressing some ctrl command.
vrinda.
 
Raymond Tanner
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vrinda,
What exactly do you mean by a shutdown hook. If someone does a Ctrl C,
there is no way of catching that IMO.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raymond,
[QB]Originally posted by Raymond Tanner:[QB]
What exactly do you mean by a shutdown hook. If someone does a Ctrl C,
there is no way of catching that IMO.

You cannot really "catch" a Control-C, however you can execute code while the system is shutting down as a result of the Control-C. So you can close your database cleanly after the Control-C, and do some logging if you wanted to. Take a look at Runtime.addShutdownHook(), or at this thread where I gave an example of some code, and made some comments on why you might want to do this.
Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic