2017-11-29 34 views
1

drop=trueを使用して終了すると、Derbyはsystem directoryを削除できません。ここでApache Derby Embedded DBシステムディレクトリを削除できません

は私の挑戦の最小限の例です:

package derbytest; 

import java.io.File; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author aelder 
*/ 
public class DerbyTest { 

    private static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver"; 
    private static final String CONN_URL = "jdbc:derby:EmbeddedDBAudit"; 

    private static File databaseFile; 
    private static final String USER_HOME_DIR = System.getProperty("user.home", "."); 

    public static Connection getConnection(boolean createDatabase) throws SQLException { 
     return DriverManager.getConnection(CONN_URL + (createDatabase ? ";create=true" : "")); 
    } 

    public static void shutdownConnectionAndCleanup() { 
     try { 
      DriverManager.getConnection(CONN_URL + ";drop=true"); 
     } catch (SQLException ex) { 
      if (!ex.getSQLState().equals("08006")) { 
       ex.printStackTrace(); 
      } 
     } 
    } 

    public static void setDerbyHome() { 
     setDatabaseFile(""); 

     int index = 1; 
     while (databaseFile.exists()) { 
      setDatabaseFile(String.valueOf(index++)); 
     } 

     // Set the db system directory. 
     System.setProperty("derby.system.home", databaseFile.getAbsolutePath()); 
    } 

    private static void setDatabaseFile(String auditFolderCount) { 
     String databaseFilePATH = USER_HOME_DIR + File.separator + ".EmbeddedDBAudit" + auditFolderCount; 

     databaseFile = new File(databaseFilePATH); 
     databaseFile.deleteOnExit(); 
    } 

    public static void initDerbyHomeAndDriver() { 
     setDerbyHome(); 

     initDerbyDriverInstance(); 
    } 

    public static void initDerbyDriverInstance() { 
     try { 
      Class.forName(DRIVER).newInstance(); 
     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) { 
      Logger.getLogger(DerbyTest.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    public static boolean tableAlreadyExists(SQLException e) { 
     return e.getSQLState().equals("X0Y32"); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     try { 
      initDerbyHomeAndDriver(); 
      getConnection(true); 
      shutdownConnectionAndCleanup(); 
     } catch (SQLException ex) { 
      Logger.getLogger(DerbyTest.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

私はこれを実行すると、私が手:

java.sql.SQLException: Directory EmbeddedDBAudit cannot be removed. 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source) 
    at org.apache.derby.jdbc.InternalDriver$1.run(Unknown Source) 
    at org.apache.derby.jdbc.InternalDriver$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at org.apache.derby.jdbc.InternalDriver.getNewEmbedConnection(Unknown Source) 
    at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source) 
    at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source) 
    at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source) 
    at java.sql.DriverManager.getConnection(DriverManager.java:664) 
    at java.sql.DriverManager.getConnection(DriverManager.java:270) 
    at derbytest.DerbyTest.shutdownConnectionAndCleanup(DerbyTest.java:34) 
    at derbytest.DerbyTest.main(DerbyTest.java:104) 
Caused by: ERROR XBM0I: Directory EmbeddedDBAudit cannot be removed. 
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source) 
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source) 
    at org.apache.derby.iapi.services.monitor.Monitor.removePersistentService(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedConnection$6.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at org.apache.derby.impl.jdbc.EmbedConnection.removePersistentService(Unknown Source) 
    ... 12 more 

コードが複数ようにシステムディレクトリの最後にカウンターを追加しよう埋め込みデータベースを使用するプログラムのインスタンスを同時に実行することができます。

user.home/.EmbeddedDBAuditで作成されたフォルダは、drop=trueが呼び出されたときに削除する必要があります。

答えて

1

表示されている動作は、文書化された動作です。

drop=trueは、永続データベースではなく、メモリ内データベースのみに使用されます。

を参照してください:http://db.apache.org/derby/docs/10.14/ref/rrefattribdrop.html

あなたは インメモリ・データベースではないデータベースでこの属性を指定した場合、DerbyはのSQLException XBM0Iが生成されます。

+0

ありがとうございます!私の質問は正しく言わなかったと思います。私がやっていることは、システムディレクトリを削除する正しい方法ではないことを知ってうれしいですが、私はまだフォルダを削除する方法を見つけることができません。 file.deleteまたはfile.deleteOnExifメソッドは、shutdown = trueの後に呼び出された場合でもfalseを返します。新しいスレッドで別のwhileループに入れて試してみてください。 –

+1

おそらく、別のStackOverflow質問としてそれを扱うべきでしょうか? –

+0

はい、あなたの回答は、他の人にとって役に立ちそうです。 –

関連する問題