Changeset 60

Show
Ignore:
Timestamp:
02/26/10 23:13:22 (6 months ago)
Author:
ke
Message:

Temporary database directory is now deleted on exit.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • kahina/trunk/src/org/kahina/core/KahinaRunner.java

    r52 r60  
    99    { 
    1010        KahinaInstance kahina = new KahinaInstance(new DbDataManager(new DatabaseHandler())); 
     11        System.err.println("Test."); 
    1112    } 
    1213} 
  • kahina/trunk/src/org/kahina/io/database/DatabaseHandler.java

    r54 r60  
    152152        { 
    153153                Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); 
    154                 File file = File.createTempFile("kahinadb", null); 
     154                final File file = File.createTempFile("kahinadb", null); 
    155155                deleteRecursively(file); 
    156156                connection = DriverManager.getConnection("jdbc:derby:" + file.getPath() 
    157157                                + ";create=true"); 
    158                 // db.deleteOnExit(); // should work but doesn't 
     158                Runtime.getRuntime().addShutdownHook(new Thread() { 
     159 
     160                        @Override 
     161                        public void run() 
     162                        { 
     163                                try 
     164                                { 
     165                                        if (!connection.isClosed()) 
     166                                        { 
     167                                                connection.close(); 
     168                                        } 
     169                                } catch (SQLException e) 
     170                                { 
     171                                        // do nothing 
     172                                } 
     173                                deleteRecursively(file); 
     174                        } 
     175                         
     176                }); 
    159177                Statement statement = connection.createStatement(); 
    160178                try 
     
    169187                                .executeUpdate("CREATE TABLE data (id BIGINT NOT NULL , value VARCHAR(32) NOT NULL, PRIMARY KEY (id))"); 
    170188                statement.close(); 
     189        } 
     190         
     191        /** 
     192         * Closes the underlying database connection. This method should always be 
     193         * invoked as soon as this handler is no longer needed. 
     194         */ 
     195        public void close() 
     196        { 
     197                try 
     198                { 
     199                        connection.close(); 
     200                } catch (SQLException e) 
     201                { 
     202                        throw new KahinaException("Failed to close database handler.", e); 
     203                } 
    171204        } 
    172205