那是什么的URLConnection的setDefaultUseCaches(假)被称为急切地在org.apache.catalina.core.JreMemoryLeak preventionListener的原因 [英] What is the reason that setDefaultUseCaches(false) of URLConnection is eagerly called in the org.apache.catalina.core.JreMemoryLeakPreventionListener
问题描述
此问题可能是一个有点难以找到答案。这是在一个系列中的问题与<一个href=\"http://stackoverflow.com/questions/7057421/why-policy-getpolicy-is-considered-as-it-will-retain-a-static-reference-to-the\">What是Policy.getPolicy()被认为是因为它会保留一个静态引用的上下文,并可能导致内存泄漏中的原因。可以读取它,所以你可以了解更多的背景。
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从<一个源$ C $ C href=\"http://grep$c$c.com/file/repo1.maven.org/maven2/org.apache.cxf/cxf-common-utilities/2.3.1/org/apache/cxf/common/logging/JDKBugHacks.java\"相对=nofollow> org.apache.cxf.common.logging.JDKBugHacks
,也从<一个href=\"http://grep$c$c.com/file/repo1.maven.org/maven2/org.apache.tomcat/tomcat-catalina/7.0.0/org/apache/catalina/core/JreMemoryLeak$p$pventionListener.java\"相对=nofollow> org.apache.catalina.core.JreMemoryLeak preventionListener
。
Graped the source code from org.apache.cxf.common.logging.JDKBugHacks
and also from org.apache.catalina.core.JreMemoryLeakPreventionListener
.
有是一块code的。这就是。
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);
的评论称
/*
* 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 operating systems.
*
* Those libraries/components known to trigger this issue
* include:
* - log4j versions 1.2.15 and earlier
* - javax.xml.bind.JAXBContext.newInstance()
*/
不过,我也很难理解。为什么他们急切地叫 setDefaultUseCaches(假)
为什么在Windows上是有害的,默认情况下缓存是真的吗?我找不到<一个任何线索href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/java/net/JarURLConnection.java\"相对=nofollow> java.net.JarURLConnection中
。
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
.
推荐答案
我自己找到答案。如果你认为我错了,任何一个可以指正。
在<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/sun/net/www/protocol/jar/JarURLConnection.java?av=h#JarURLConnection\"相对=nofollow> sun.net.www.protocol.jar.JarURLConnection
。我想这是 java.net.JarURLConnection中
的默认实现。有一块低于code的。
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.
如果缓存设置为true,那么就不会关闭jar文件的连接。这意味着它已被锁定。
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;
}
}
这篇关于那是什么的URLConnection的setDefaultUseCaches(假)被称为急切地在org.apache.catalina.core.JreMemoryLeak preventionListener的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!