• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About File close question

 
Jesse Xie Y.S.
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11945
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    Jesse Xie Y.S.
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 11945
    212
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Jesse Xie Y.S.
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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();
    }
    }
    }


     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 11945
    212
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Jesse,

    Yep - that's what I was suggesting.

    Regards, Andrew
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic