Browse Source

SQL: JDBC: fix temporary directory locked test errors in Windows (#56917)

* Fix temp dir locked errors

The tests involving a temporary directory (containing the JDBC JAR) fail
on Windows because they can't be deleted, due to still being in use.
This commit forces a premature closing of the JAR file, which mitigates
the failure by giving the JVM more time to collect any open FDs.
(Calling the System.gc() in the tests is another working alternative
fix.)

The stream-based JAR access is taken care by disabling the cache usage
Bogdan Pintea 5 years ago
parent
commit
04f97333a0

+ 5 - 1
x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/ClientVersion.java

@@ -16,6 +16,7 @@ import java.util.Enumeration;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.jar.JarFile;
 import java.util.jar.JarInputStream;
 import java.util.jar.Manifest;
 
@@ -76,6 +77,7 @@ public class ClientVersion {
     // (1) a file URL: file:<path><FS separator><driver name>.jar
     // (2) jar file URL pointing to a JAR file: jar:<sub-url><separator><driver name>.jar!/
     // (3) jar file URL pointing to a JAR file entry (likely a fat JAR, but other types are possible): jar:<sub-url>!/driver name>.jar!/
+    @SuppressForbidden(reason="java.util.jar.JarFile must be explicitly closed on Windows")
     static Manifest getManifest(URL url) throws IOException {
         String urlStr = url.toString();
         if (urlStr.endsWith(".jar") || urlStr.endsWith(".jar!/")) {
@@ -87,7 +89,9 @@ public class ClientVersion {
             if (url.getProtocol().equals("jar")) {
                 JarURLConnection jarConn = (JarURLConnection) conn;
                 if (jarConn.getEntryName() == null) { // the URL points to a JAR file
-                    return jarConn.getManifest(); // in case of a fat JAR, this would return the outermost JAR's manifest
+                    try (JarFile jar = jarConn.getJarFile()) { // prevent locked file errors in Windows.
+                        return jar.getManifest(); // in case of a fat JAR, this would return the outermost JAR's manifest
+                    }
                 }
             }
             try (JarInputStream jar = new JarInputStream(conn.getInputStream())) {

+ 0 - 2
x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/VersionTests.java

@@ -5,7 +5,6 @@
  */
 package org.elasticsearch.xpack.sql.client;
 
-import org.apache.lucene.util.LuceneTestCase;
 import org.elasticsearch.test.ESTestCase;
 import org.elasticsearch.xpack.sql.proto.SqlVersion;
 
@@ -20,7 +19,6 @@ import java.util.jar.JarEntry;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
 
-@LuceneTestCase.AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/56882")
 public class VersionTests extends ESTestCase {
 
     public void testCurrent() {