1 5 package org.h2.engine; 6 7 import java.lang.ref.WeakReference ; 8 9 public class DatabaseCloser extends Thread { 10 private WeakReference databaseRef; 11 private int delayInMillis; 12 private boolean shutdownHook; 13 14 DatabaseCloser(Database db, int delayInMillis, boolean shutdownHook) { 15 this.databaseRef = new WeakReference (db); 16 this.delayInMillis = delayInMillis; 17 this.shutdownHook = shutdownHook; 18 } 19 20 public void reset() { 21 synchronized(this) { 22 databaseRef = null; 23 } 24 } 25 26 public void run() { 27 while(delayInMillis > 0) { 28 try { 29 int step = 100; 30 Thread.sleep(step); 31 delayInMillis -= step; 32 } catch(Exception e) { 33 } 35 if(databaseRef == null) { 36 return; 37 } 38 } 39 synchronized(this) { 40 if(databaseRef != null) { 41 Database database = (Database) databaseRef.get(); 42 if(database != null) { 43 database.close(shutdownHook); 44 } 45 } 46 } 47 } 48 49 } 50 | Popular Tags |