开发者

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

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 static reference to the context and can cause memory leak. You can read it so you may know more background.

Graped the source code from org.apache.cxf.common.logging.JDKBugHacks and also from org.apache.catalina.core.JreMemoryLeakPreventionListener.

There is a piece of code. Here it is.

URL url = new URL("jar:file://dummy.jar!/"); 
URLConnection uConn = new URLConnection(url) {

    @Override
    public void connect() throws IOException{ 
        // NOOP
    }

};
uConn.setDefaultUseCaches(false);

The comment said

/*
 * Several components end up opening JarURLConnections without
 * first disabling caching. This effectively locks the file.
 * Whilst more noticeable and harder to ignore on Windows, it
 * affects all opera开发者_如何学JAVAting systems.
 * 
 * Those libraries/components known to trigger this issue
 * include:
 * - log4j versions 1.2.15 and earlier
 * - javax.xml.bind.JAXBContext.newInstance()
 */

However I can hardly understand it. Why did they eagerly call setDefaultUseCaches(false) and why on Windows it's harmful that by default cache is true? I cannot find any clue in java.net.JarURLConnection.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜