aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes About File close question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "About File close question" Watch "About File close question" New topic
Author

About File close question

Jesse Xie Y.S.
Greenhorn

Joined: Feb 09, 2004
Posts: 20
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.


MOON -- SCJP1.2
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

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


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    Jesse Xie Y.S.
    Greenhorn

    Joined: Feb 09, 2004
    Posts: 20
    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

    Joined: Mar 28, 2003
    Posts: 11481
        
      94

    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

    Joined: Feb 09, 2004
    Posts: 20
    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

    Joined: Mar 28, 2003
    Posts: 11481
        
      94

    Hi Jesse,

    Yep - that's what I was suggesting.

    Regards, Andrew
     
    jQuery in Action, 2nd edition
     
    subject: About File close question