What is the reason that setDefaultUseCaches(false) of URLConnection is eagerly called in the org.apache.catalina.core.JreMemoryLeakPreventionListener

后端 未结 1 487
说谎
说谎 2021-01-16 06:53

This question could be a bit difficult to find the answer. It\'s a questions in one series with What is the reason that Policy.getPolicy() is considered as it will retain a

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-16 07:49

    I myself find an answer. Any one can correct me if you think I am wrong. in sun.net.www.protocol.jar.JarURLConnection. I assume this is the default implementation of java.net.JarURLConnection. There is a piece of code below.

    If cache is set to true, then it will not close the JarFile's connection. Which means it is locked.

    class JarURLInputStream extends java.io.FilterInputStream {
            JarURLInputStream (InputStream src) {
                super (src);
            }
            public void close () throws IOException {
                try {
                    super.close();
                } finally {
                    if (!getUseCaches()) {
                        jarFile.close(); //will not close
                    }
                }
            }
        }
    
    
    
        public void connect() throws IOException {
            if (!connected) {
                /* the factory call will do the security checks */
                jarFile = factory.get(getJarFileURL(), getUseCaches());
    
                /* we also ask the factory the permission that was required
                 * to get the jarFile, and set it as our permission.
                 */
                if (getUseCaches()) {
                    jarFileURLConnection = factory.getConnection(jarFile);
                }
    
                if ((entryName != null)) {
                    jarEntry = (JarEntry)jarFile.getEntry(entryName);
                    if (jarEntry == null) {
                        try {
                            if (!getUseCaches()) {
                                jarFile.close();  //will not close
                            }
                        } catch (Exception e) {
                        }
                        throw new FileNotFoundException("JAR entry " + entryName +
                                                        " not found in " +
                                                        jarFile.getName());
                    }
                }
                connected = true;
            }
        }
    

    0 讨论(0)
提交回复
热议问题