Linux 使用 javax.xml 的 Java 中的错误文件描述符 IOException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10286233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:57:03  来源:igfitidea点击:

Bad File Descriptor IOException in Java using javax.xml

javaxmllinuxfilexml-parsing

提问by Ina

I'm using the standard javax.xml package to parse some XML files on a linux machine. My code is as follows:

我正在使用标准的 javax.xml 包来解析 Linux 机器上的一些 XML 文件。我的代码如下:

try 
{
    // Prepare parser
    DocumentBuilder documentBuilder = documentBuilderFactory
        .newDocumentBuilder();
    Document document = documentBuilder.parse(file.getAbsolutePath()); // This is line 397
    XPath xPath = xPathFactory.newXPath();
    ...
}
catch(IOException e) { ... }

A single DocumentBuilderFactory is accessed by multiple threads, as is a single XPathFactory, I believe this to be acceptable usage. I occasionally see the following error when parsing an XML file using the above code.

单个 DocumentBuilderFactory 由多个线程访问,单个 XPathFactory 也是如此,我相信这是可以接受的用法。使用上述代码解析 XML 文件时,我偶尔会看到以下错误。

java.io.IOException: Bad file descriptor
        at java.io.FileInputStream.readBytes(Native Method)
        at java.io.FileInputStream.read(FileInputStream.java:229)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:229)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:246)
        at org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
        at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
        at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
        at mypackage.MyXmlParser.parseFile(MyXmlParser.java:397)
        at mypackage.MyXmlParser.access0(MyXmlParser.java:51)
        at mypackage.MyXmlParser.call(MyXmlParser.java:337)
        at mypackage.MyXmlParser.call(MyXmlParser.java:328)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:284)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:665)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:690)
        at java.lang.Thread.run(Thread.java:799)

I occasionally (~10% of the time) see the following additional text:

我偶尔(约 10% 的时间)会看到以下附加文本:

Caused by:
java.io.IOException: Bad file descriptor
        at org.apache.xml.serializer.ToStream.flushWriter(ToStream.java:260)
        at org.apache.xml.serializer.ToXMLStream.endDocument(ToXMLStream.java:191)
        at org.apache.xalan.transformer.TransformerIdentityImpl.endDocument(TransformerIdentityImpl.java:983)
        at org.apache.xml.serializer.TreeWalker.traverse(TreeWalker.java:174)
        at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:410)
        ... 9 more

When I inspect the files manually, I can see no difference between the files that fail and the files that pass. I can confirm the files that pass are valid XML and have no special characters or premature endings.

当我手动检查文件时,我看不出失败的文件和通过的文件之间没有区别。我可以确认通过的文件是有效的 XML,并且没有特殊字符或过早结束。

Does anyone know why this might be happening, and how I can avoid it?

有谁知道为什么会发生这种情况,以及我如何避免它?

> java -version
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build pxa64dev-20061002a (SR3) )
IBM J9 VM (build 2.3, J2RE 1.5.0 IBM J9 2.3 Linux amd64-64 j9vmxa6423-20061001 (JIT enabled)
J9VM - 20060915_08260_LHdSMr
JIT  - 20060908_1811_r8
GC   - 20060906_AA)
JCL  - 20061002

采纳答案by Johanna

It looks like an issue with concurrent threads.

看起来像是并发线程的问题。

The error can be somewhere outside the codelet which you show us. But also with DocumentBuilderFactory and XPathFactory I'm not sure if they are thread-safe; it is not mentioned in the documentation.

错误可能在您向我们展示的小码之外的某个地方。但是对于 DocumentBuilderFactory 和 XPathFactory,我不确定它们是否是线程安全的;文档中没有提到它。

For a first test I recommend to you to put the whole code for parsing XML files into a synchronized {}clause. If this solves your problem, then it definitively is a multithread problem. In this case you have to find out the smallest part of code which must be synchronized.

对于第一个测试,我建议您将解析 XML 文件的整个代码放入一个synchronized {}子句中。如果这解决了您的问题,那么它绝对是一个多线程问题。在这种情况下,您必须找出必须同步的最小代码部分。