[英]Unable to delete Derby System Directory for Embedded Database
在Windows計算機上為Derby Embedded數據庫調用shutdown = true時,我無法刪除系統目錄 。
這是我挑戰的最小例子:
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 derbySystemFolder;
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 + ";shutdown=true");
} catch (SQLException ex) {
if (!ex.getSQLState().equals("08006")) {
ex.printStackTrace();
}
}
deleteFolder(derbySystemFolder);
}
public static void deleteFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) { //some JVMs return null for empty dirs
for (File f : files) {
if (f.isDirectory()) {
deleteFolder(f);
} else {
f.delete();
}
}
}
folder.delete();
}
public static void setDerbyHome() {
setDatabaseFile("");
int index = 1;
while (derbySystemFolder.exists()) {
setDatabaseFile(String.valueOf(index++));
}
// Set the db system directory.
System.setProperty("derby.system.home", derbySystemFolder.getAbsolutePath());
}
private static void setDatabaseFile(String auditFolderCount) {
String databaseFilePATH = USER_HOME_DIR + File.separator + ".EmbeddedDBAudit" + auditFolderCount;
derbySystemFolder = new File(databaseFilePATH);
derbySystemFolder.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);
}
}
}
我已經嘗試使用外部庫來刪除文件夾,例如apache的org.apache.commons.io.FileDeleteStrategy.FORCE.delete(file);
或者import org.apache.commons.io.FileUtils.deleteDirectory(file);
。 即使在數據庫關閉后,似乎derby系統仍然掛起到文件上。
所需行為:退出時刪除系統目錄 。
編輯:
Windows進程資源管理器向我顯示在數據庫連接關閉后,derby.log仍處於打開狀態:
假設它的Derby代碼本身無法關閉此文件,這對我來說似乎是個錯誤。
關閉Engine之后,Derby顯然應該關閉它的日志並釋放它的所有引用。
如果您可以訪問https://www.eclipse.org/mat/這樣的工具,您可能能夠找到哪個線程(甚至是哪個堆棧框架?)已將此引用分配給derby.log並且無法釋放它。
如果您可以將此行為封裝在一個演示它的小型測試程序中,我建議您記錄針對Derby的錯誤( http://db.apache.org/derby/DerbyBugGuidelines.html ),以便開發人員可以自己重現它弄清楚如何解決它。
在此期間,您可以通過禁用derby.log文件或將derby.log文件重定位到另一個目錄來解決此問題。
這顯然不是一個解決方案,但它可能是一種行為的改善,以至於這個缺陷不再阻礙你的工作了? 以下是有關如何控制derby.log文件的一些文檔: https ://builds.apache.org/job/Derby-docs/lastSuccessfulBuild/artifact/trunk/out/devguide/cdevdvlp25889.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.