On Friday 11 April 2008 08:17, j16sdiz at freenetproject.org wrote:
> Author: j16sdiz
> Date: 2008-04-11 07:17:31 +0000 (Fri, 11 Apr 2008)
> New Revision: 19161
> 
> Modified:
>    trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java
> Log:
> simplify BDBFS.close exception handling

IMHO in this instance we do need to catch (Throwable), in order to close as 
much as we can and avoid resource leaks. No?
> 
> 
> Modified: trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java
> ===================================================================
> --- trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java       
> 2008-04-11 
07:17:09 UTC (rev 19160)
> +++ trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java       
> 2008-04-11 
07:17:31 UTC (rev 19161)
> @@ -67,12 +67,12 @@
>       private long misses = 0;
>       private long writes = 0;
>       private final int keyLength;
> -     private final Database keysDB;
> -     private final SecondaryDatabase accessTimeDB;
> -     private final SecondaryDatabase blockNumDB;
> -     private final RandomAccessFile storeRAF;
> -     private final RandomAccessFile keysRAF;
> -     private final RandomAccessFile lruRAF;
> +     private Database keysDB;
> +     private SecondaryDatabase accessTimeDB;
> +     private SecondaryDatabase blockNumDB;
> +     private RandomAccessFile storeRAF;
> +     private RandomAccessFile keysRAF;
> +     private RandomAccessFile lruRAF;
>       private final SortedLongSet freeBlocks;
>       private final String name;
>       /** Callback which translates records to blocks and back, specifies the 
size of blocks etc. */
> @@ -1582,6 +1582,30 @@
>       }
>       
>       private final Object closeLock = new Object();
> +
> +     private void closeRAF(RandomAccessFile file, boolean logError) {
> +                     try {
> +                                             if (file != null)
> +                                                             file.close();
> +                             } catch (IOException e) {
> +                                             if (logError) {
> +                                                             
> System.err.println("Caught closing file: " + e);
> +                                                             
> e.printStackTrace();
> +                                             }
> +                             }
> +             }
> +
> +     private void closeDB(Database db, boolean logError) {
> +                             try {
> +                                             if (db != null)
> +                                                             db.close();
> +                             } catch (DatabaseException e) {
> +                                             if (logError) {
> +                                                             
> System.err.println("Caught closing database: " + e);
> +                                                             
> e.printStackTrace();
> +                                             }
> +                             }
> +             }
>       
>       private void close(boolean sleep) {
>               try {
> @@ -1598,7 +1622,11 @@
>  
>                       // Give all threads some time to complete
>                       if(sleep)
> +                             try {
>                               Thread.sleep(5000);
> +                             } catch (InterruptedException ie) {
> +                                     Logger.error(this, "Thread 
> interrupted.", ie);
> +                             }
>                       
>                       if(reallyClosed) {
>                               Logger.error(this, "Already closed "+this);
> @@ -1611,57 +1639,28 @@
>                                       return;
>                               }
>                               
> -                             try {
> -                                     if(storeRAF != null)
> -                                             storeRAF.close();
> -                                     if(lruRAF != null)
> -                                             lruRAF.close();
> -                                     if(keysRAF != null)
> -                                             keysRAF.close();
> -                             } catch (Throwable t) {
> -                                     if(!(t instanceof RunRecoveryException 
> || t instanceof 
OutOfMemoryError)) {
> -                                             System.err.println("Caught 
> closing database: "+t);
> -                                             t.printStackTrace();
> -                                     }
> -                             }
> -                             try {
> -                                     if(accessTimeDB != null)
> -                                             accessTimeDB.close();
> -                             } catch (Throwable t) {
> -                                     if(!(t instanceof RunRecoveryException 
> || t instanceof 
OutOfMemoryError)) {
> -                                             System.err.println("Caught 
> closing database: "+t);
> -                                             t.printStackTrace();
> -                                     }
> -                             }
> -                             try {
> -                                     if(blockNumDB != null)
> -                                             blockNumDB.close();
> -                             } catch (Throwable t) {
> -                                     if(!(t instanceof RunRecoveryException 
> || t instanceof 
OutOfMemoryError)) {
> -                                             System.err.println("Caught 
> closing database: "+t);
> -                                             t.printStackTrace();
> -                                     }
> -                             }
> -                             try {   
> -                                     if(keysDB != null)
> -                                             keysDB.close();
> -                             } catch (Throwable t) {
> -                                     if(!(t instanceof RunRecoveryException 
> || t instanceof 
OutOfMemoryError)) {
> -                                             System.err.println("Caught 
> closing database: "+t);
> -                                             t.printStackTrace();
> -                                     }
> -                             }
> +                             closeRAF(storeRAF, true);
> +                             storeRAF = null;
> +                             closeRAF(lruRAF, true);
> +                             lruRAF = null;
> +                             closeRAF(keysRAF, true);
> +                             keysRAF = null;
> +
> +                             closeDB(accessTimeDB, true);
> +                             accessTimeDB = null;
> +                             closeDB(blockNumDB, true);
> +                             blockNumDB = null;
> +                             closeDB(keysDB, true);
> +                             keysDB = null;
> +
>                               if(logMINOR) Logger.minor(this, "Closing 
> database finished.");
>                               System.err.println("Closed database");
> -                             reallyClosed = true;
>                       }
> -             } catch(Throwable ex) {
> -                     try {
> -                             Logger.error(this,"Error while closing 
> database.",ex);
> -                             ex.printStackTrace();
> -                     } catch (Throwable t) {
> -                             // Return anyway
> -                     }
> +             } catch (RuntimeException ex) {
> +                     Logger.error(this, "Error while closing database.", ex);
> +                     ex.printStackTrace();
> +             } finally {
> +                     reallyClosed = true;
>               }
>       }
>       
> 
> _______________________________________________
> cvs mailing list
> cvs at freenetproject.org
> http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs
> 
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: 
<https://emu.freenetproject.org/pipermail/cvs/attachments/20080411/d44bb58d/attachment.pgp>

Reply via email to