使用drop=true
退出时,Derby无法删除system directory。
以下是我挑战的最小例子:
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
创建的文件夹。
答案 0 :(得分:1)
您看到的行为是记录在案的行为。
drop=true
仅用于内存数据库,而不是持久数据库。
请参阅:http://db.apache.org/derby/docs/10.14/ref/rrefattribdrop.html
如果使用不是的数据库指定此属性 在内存数据库中,Derby生成SQLException XBM0I。