Forums Register Login

About File close question

+Pie Number of slices to send: Send
In my DBAccess interface, there is no resource release method such as close, dispose, release etc for me to close the opening file (I open the specified database file by RandomAccessFile and don't close it util the program exit), how can i close the file before the program exit or when the user change the database file name.
+Pie Number of slices to send: Send
Hi Jesse,

Two of the common methods are:
  • Have your Data class implement an additional interface that defines a method for closing the database. Before shutting down your server you can confirm that your instance of the Data class is an instanceof your additional interface and call the close method (stopping any writes from happening)
  • Close the database in the finalize method (dangerous)
  • Use a "magic" record number (e.g. -1) that when locked causes the database to be closed
  • As you can see, I can't count

    Regards, Andrew
    +Pie Number of slices to send: Send
    I got it:

    1.
    public class Data implements DBAccess{
    RandomAccessFile dataFile;
    ... ...
    public void close(){
    try{
    dataFile.close;
    }catch(IOException ioe){
    //ignore this exception
    }
    }
    ... ...
    }

    public class Server{
    DBAccess dbAccess;
    ... ...
    public void stop(){
    if(dbAccess instanceof Data)
    try{
    ((Data)dbAccess).close();
    }catch(IOException ioe){
    //ignore exception
    }
    }
    ... ...
    }

    How do you think?
    +Pie Number of slices to send: Send
    Hi Jessie,

    That will work, however it is only guaranteed to work with your version of the Data class.

    As things stand, if somebody plugged in a different version of the Data class (not that it is really practical ), then your program should appear to work perfectly (since the other version of Data will still be meeting the contractual requirements of DBAccess), however right at the last moment - when you go to shut down the server - your application will crash because it tried to call a non existant method in Data class.

    As mentioned though, it is not really practical for anybody to just replace your Data class in the way that I have suggested. So your method of handling this may be acceptable, but you would probably want to note it in your design decisions document.

    My first suggestion was to incorporate another interface, so that instead of:You would haveYou can then verify that your Data class is an instanceof DBShutdown before calling the close method.

    Regards, Andrew
    +Pie Number of slices to send: Send
    OK, this time, I really got it:

    /**
    * A class implements the Closeable interface to indicate to the close method
    * that it is legal for that method to make a resource release for this class.
    *
    * @version 1.1.1, 2005-8-27
    * @author Johnson Xie
    */

    public interface Closeable {

    /**
    * Release the resources in this object.
    */
    public void close();
    }


    public class Data implements DBAccess, Closeable {
    ... ...
    }

    public interface URLyBirdService {
    ... ...
    }

    public class URLyBirdServiceImpl implements URLyBirdService {
    private DBAccess dbAccess;
    ... ...
    public void close(){

    if(dbAccess instanceof Closeable){

    ((Closeable)dbAccess).close();
    }
    }
    }


    +Pie Number of slices to send: Send
    Hi Jesse,

    Yep - that's what I was suggesting.

    Regards, Andrew
    I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 987 times.
    Similar Threads
    Writing from variables into a file
    Do you close the database (RAF) when exiting?
    Nonmodal window showing image
    Cant seem to get this to run as an applet?
    Replacing jar file, while still running old jar
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 01:52:32.