简体   繁体   English

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

[英]Bad File Descriptor IOException in Java using javax.xml

I'm using the standard javax.xml package to parse some XML files on a linux machine. 我正在使用标准的javax.xml包来解析linux机器上的一些XML文件。 My code is as follows: 我的代码如下:

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. 单个DocumentBuilderFactory由多个线程访问,单个XPathFactory也是如此,我相信这是可接受的用法。 I occasionally see the following error when parsing an XML file using the above code. 使用上面的代码解析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.access$500(MyXmlParser.java:51)
        at mypackage.MyXmlParser$1.call(MyXmlParser.java:337)
        at mypackage.MyXmlParser$1.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

It looks like an issue with concurrent threads. 它看起来像并发线程的问题。

The error can be somewhere outside the codelet which you show us. 错误可能在您向我们展示的codelet之外的某处。 But also with DocumentBuilderFactory and XPathFactory I'm not sure if they are thread-safe; 但是对于DocumentBuilderFactory和XPathFactory,我不确定它们是否是线程安全的; it is not mentioned in the documentation. 它没有在文档中提到。

For a first test I recommend to you to put the whole code for parsing XML files into a synchronized {} clause. 对于第一个测试,我建议您将用于解析XML文件的整个代码放入synchronized {}子句中。 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. 在这种情况下,您必须找出必须同步的最小代码部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将XML文件(InputStream)传递给XSLT避免在XSLT javax.xml中使用Document - Passing a XML File (InputStream) to XSLT avoid using Document in XSLT javax.xml java.io.IOException:读取失败:EBADF(错误的文件描述符) - java.io.IOException: read failed: EBADF (Bad file descriptor) 使用javax.xml添加新的POJO时发生ClassCastException - ClassCastException when adding new POJO using javax.xml TransformerFactory.setAttribute的javax.xml属性 - javax.xml attributes of TransformerFactory.setAttribute Indigo目标平台缺少javax.xml - Indigo Target Platform missing javax.xml Android Java IOException从文件描述符/输入流中读取失败的EBADF(错误文件编号) - Android Java IOException read failed EBADF (Bad File Number) From File descriptor/Input Stream log4j失败-无法刷新Servlet中的编写器(java.io.IOException:错误的文件描述符) - log4j failure - Failed to flush writer in servlet (java.io.IOException: Bad file descriptor) java.io.IOException:错误的文件描述符Jetty 9.2.10.v20150310 - java.io.IOException: Bad file descriptor Jetty 9.2.10.v20150310 javax.xml,不会从具有名称空间的XML中提取XPath - javax.xml, XPath is not extracted from XML with namespaces 获取XML文档中使用的前缀/名称空间绑定,最好使用org.w3c.dom和javax.xml API - get prefix / namespace bindings used in an XML document preferably using org.w3c.dom and javax.xml APIs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM