From 6d34c4ddfa465acc3456b264a2be4d2d7c56ce1c Mon Sep 17 00:00:00 2001 From: Markus Spann Date: Tue, 26 Nov 2024 18:36:40 +0100 Subject: [PATCH] Harmonize file creation, add logging --- .../java/net/ucanaccess/jdbc/DBReference.java | 27 +++++++++++++------ .../net/ucanaccess/jdbc/UcanaccessDriver.java | 3 +-- .../jdbc/ReloadPersistentMirrorTest.java | 2 +- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/ucanaccess/jdbc/DBReference.java b/src/main/java/net/ucanaccess/jdbc/DBReference.java index 4671442..a29af3b 100644 --- a/src/main/java/net/ucanaccess/jdbc/DBReference.java +++ b/src/main/java/net/ucanaccess/jdbc/DBReference.java @@ -249,7 +249,9 @@ private void closeHsqlDb(Session _session, boolean _firstConnectionKeeptMirror) hbase.delete(); } else if (!immediatelyReleaseResources || _firstConnectionKeeptMirror) { toKeepHsql.delete(); - if (!toKeepHsql.createNewFile()) { + if (toKeepHsql.createNewFile()) { + logger.log(Level.DEBUG, "Created file {0}", toKeepHsql); + } else { logger.log(Level.WARNING, "Could not create file {0}", toKeepHsql); } for (File hsqlf : getHSQLDBFiles()) { @@ -368,8 +370,12 @@ private String getHsqlUrl(Session _session) throws SQLException { } if (!inMemory && tempHsql == null) { if (toKeepHsql != null) { - if (!toKeepHsql.exists() && !toKeepHsql.createNewFile()) { - logger.log(Level.WARNING, "Could not create file {0}", toKeepHsql); + if (!toKeepHsql.exists()) { + if (toKeepHsql.createNewFile()) { + logger.log(Level.DEBUG, "Created file {0}", toKeepHsql); + } else { + logger.log(Level.WARNING, "Could not create file {0}", toKeepHsql); + } } tempHsql = toKeepHsql; } else { @@ -378,10 +384,13 @@ private String getHsqlUrl(Session _session) throws SQLException { hbase.mkdir(); tempHsql = new File(hbase, id); - if (!tempHsql.createNewFile()) { - logger.log(Level.WARNING, "Could not create file {0}", tempHsql); - } else { - tempHsql.delete(); + if (!tempHsql.exists()) { + if (tempHsql.createNewFile()) { + logger.log(Level.DEBUG, "Created file {0}", tempHsql); + tempHsql.delete(); + } else { + logger.log(Level.WARNING, "Could not create file {0}", tempHsql); + } } } Runtime.getRuntime().addShutdownHook(new Thread(() -> { @@ -446,7 +455,9 @@ private File fileLock() { private void lockMdbFile() throws UcanaccessSQLException { try { File flLock = fileLock(); - if (!flLock.createNewFile()) { + if (flLock.createNewFile()) { + logger.log(Level.DEBUG, "Created file {0}", flLock); + } else { logger.log(Level.WARNING, "Could not create file {0}", flLock); } diff --git a/src/main/java/net/ucanaccess/jdbc/UcanaccessDriver.java b/src/main/java/net/ucanaccess/jdbc/UcanaccessDriver.java index e77163a..c4d657b 100644 --- a/src/main/java/net/ucanaccess/jdbc/UcanaccessDriver.java +++ b/src/main/java/net/ucanaccess/jdbc/UcanaccessDriver.java @@ -108,8 +108,7 @@ public Connection connect(String _url, Properties _props) throws SQLException { LOGGER.log(Level.WARNING, "{0} parameter cannot be combined with parameters {1} or {2}, {3} skipped", keepMirror, jackcessOpener, encrypt, keepMirror); } else { - File dbMirror = - new File(props.get(keepMirror) + fileDb.getName().toUpperCase().hashCode()); + File dbMirror = new File(props.get(keepMirror)); dbRef.setToKeepHsql(dbMirror); if (props.containsKey(readOnlyMirror)) { dbRef.setMirrorReadOnly(Boolean.parseBoolean(props.get(readOnlyMirror))); diff --git a/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java b/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java index 548428e..d35d0a5 100644 --- a/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java +++ b/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java @@ -20,7 +20,7 @@ void testReloadMirror(AccessVersion _accessVersion) throws Exception { File dbFile = createTempFileName(getClass().getSimpleName(), getFileExtension()); dbFile.deleteOnExit(); - File mirrorFile = createTempFileName(getClass().getSimpleName(), ""); + File mirrorFile = new File(dbFile.getAbsolutePath().substring(0, dbFile.getAbsolutePath().lastIndexOf(getFileExtension()))); mirrorFile.deleteOnExit(); // create the database