[
https://issues.apache.org/jira/browse/DERBY-7150?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Liam Sharp closed DERBY-7150.
-----------------------------
Resolution: Not A Bug
> derby.log locked after database shutdown preventing deletion
> ------------------------------------------------------------
>
> Key: DERBY-7150
> URL: https://issues.apache.org/jira/browse/DERBY-7150
> Project: Derby
> Issue Type: Bug
> Affects Versions: 10.16.1.1
> Environment: Windows
> Reporter: Liam Sharp
> Priority: Major
>
> On Windows I'm unable to delete derby.log after shutting down the database.
>
> I've created a demo maven project
> [here|https://github.com/screamingfrog/hello-world-cli/tree/derby-locking-logfile],
> that if run on Windows, produces the following:
>
> {{2023-02-03 13:16:58,070 [main] INFO - derby.system.home will be set to:
> C:\Users\Administrator\derby-test}}
> {{2023-02-03 13:16:58,073 [main] INFO - Folder exists, deleting}}
> {{2023-02-03 13:16:58,073 [main] INFO - Initalising driver}}
> {{2023-02-03 13:16:58,189 [main] INFO - Creating connection
> jdbc:derby:EmbeddedDBAudit;create=true}}
> {{2023-02-03 13:16:58,955 [main] INFO - Shutting down connection
> jdbc:derby:EmbeddedDBAudit;shutdown=true}}
> {{2023-02-03 13:16:58,978 [main] WARN - Failed to delete file
> C:\Users\Administrator\derby-test\derby.log}}
>
> Code inline is here:
>
> {{{}package com.github.screamingfrog;{}}}{{{}import java.io.File;{}}}
> {{import java.sql.DriverManager;}}
> {{{}import java.sql.SQLException;{}}}{{{}import
> org.apache.logging.log4j.LogManager;{}}}
> {{{}import org.apache.logging.log4j.Logger;{}}}{{{}public class App {}}}
> {{{}}{{ private static final Logger LOGGER =
> LogManager.getLogger(App.class);}}
> {{ }}
> {{ private static final String DRIVER =
> "org.apache.derby.jdbc.EmbeddedDriver";}}{{ private static final String
> CONNECTION_URL = "jdbc:derby:EmbeddedDBAudit";}}
> {{ private static final String CREATE_CONNECTION_URL = CONNECTION_URL +
> ";create=true";}}
> {{ private static final String SHUTDOWN_CONNECTION_URL = CONNECTION_URL +
> ";shutdown=true";}}{{ private static File derbySystemFolder;}}{{ public
> static void main(}}
> {{ String[] args) }}
> {{ {}}
> {{ try }}
> {{ {}}
> {{ initDerbyHomeAndDriver();}}
> {{ createConnection();}}
> {{ shutdownConnectionAndCleanup();}}
> {{ }}}
> {{ catch (SQLException e) }}
> {{ {}}
> {{ LOGGER.error("SQLException: " + e, e);}}
> {{ }}}
> {{ }}}{{ private static void initDerbyHomeAndDriver() }}
> {{ {}}
> {{ setDerbyHome();}}
> {{ initDerbyDriverInstance();}}
> {{ }}}{{ private static void createConnection() throws SQLException }}
> {{ {}}
> {{ LOGGER.info("Creating connection " + CREATE_CONNECTION_URL);}}
> {{ DriverManager.getConnection(CREATE_CONNECTION_URL);}}
> {{ }}}{{ private static void shutdownConnectionAndCleanup() }}
> {{ {}}
> {{ LOGGER.info("Shutting down connection " +
> SHUTDOWN_CONNECTION_URL);}}
> {{ try }}
> {{ {}}
> {{ DriverManager.getConnection(SHUTDOWN_CONNECTION_URL);}}
> {{ }}}
> {{ catch (SQLException e) }}
> {{ {}}
> {{ if (!e.getSQLState().equals("08006"))}}
> {{ {}}
> {{ LOGGER.error("Failed to shutdown database " + e, e);}}
> {{ }}}
> {{ }}}{{ deleteFolder(derbySystemFolder);}}
> {{ }}}{{ private static void setDerbyHome() }}
> {{ {}}
> {{ derbySystemFolder = new File (System.getProperty("user.home") +
> File.separator + "derby-test");}}
> {{ LOGGER.info("derby.system.home will be set to: " +
> derbySystemFolder);}}
> {{ }}
> {{ if (derbySystemFolder.exists()) }}
> {{ {}}
> {{ LOGGER.info("Folder exists, deleting");}}
> {{ deleteFolder(derbySystemFolder);}}
> {{ }}}
> {{ System.setProperty("derby.system.home",
> derbySystemFolder.getAbsolutePath());}}
> {{ }}}{{ private static void initDerbyDriverInstance() }}
> {{ {}}
> {{ LOGGER.info("Initalising driver");}}
> {{ try }}
> {{ {}}
> {{ Class.forName(DRIVER).newInstance();}}
> {{ }}}
> {{ catch (ClassNotFoundException | InstantiationException |
> IllegalAccessException e) }}
> {{ {}}
> {{ LOGGER.error("Failed to init " + DRIVER + " " + e, e);}}
> {{ }}}
> {{ }}}{{ private static void deleteFolder(}}
> {{ final File folder) }}
> {{ {}}
> {{ LOGGER.debug("Deleting folder " + folder);}}
> {{ File[] files = folder.listFiles();}}
> {{ if (files != null) }}
> {{ {}}
> {{ for (File f : files) }}
> {{ {}}
> {{ if (f.isDirectory()) }}
> {{ {}}
> {{ deleteFolder(f);}}
> {{ } }}
> {{ else }}
> {{ {}}
> {{ LOGGER.debug("Deleting file " + f);}}
> {{ if (! f.delete())}}
> {{ {}}
> {{ LOGGER.warn("Failed to delete file " + f);}}
> {{ }}}
> {{ }}}
> {{ }}}
> {{ }}}
> {{ folder.delete();}}
> {{ } }}
> {{}}}
>
> Looks like this might be a long standing issue as I found this
> [StackOverflow|https://stackoverflow.com/questions/47582819/unable-to-delete-derby-system-directory-for-embedded-database]
> from 2017.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)