javaexcelresourcesjxlsresource-leak

Does the XLSReader from JXLS close the workbook?


I would like to know if the XlsReader class from jxls closes the workbook. My code is something like this:

XLSReader mainReader = ReaderBuilder.buildFromXML(inputStreamXml);
mainReader.read(inputStreamXLS, beans);

There's no method like close available in XLSReader. I tried to find some point in the internal code when the workbook is closed but I couldn't find. This is the internal implementation of the read method:

public XLSReadStatus read(InputStream inputXLS, Map beans) throws 
        IOException, InvalidFormatException{
        readStatus.clear();
        Workbook workbook = WorkbookFactory.create(inputXLS);
        for(int sheetNo = 0; sheetNo < workbook.getNumberOfSheets(); sheetNo++){
            readStatus.mergeReadStatus(readSheet(workbook, sheetNo, beans));
        }
        return readStatus;
}

Sometimes the xls file is locked by the application, since I couldn't find any point in my code where the resource is not released, I wonder if the problem is the jxls. Apparently the XLSReader doesn't close the workbook. My application closes the inputStreams. Does the application also need to close the workbook?


Solution

  • XLSReader does not close the workbook. The reason is that jxls-reader is rather an old module and at the time of its creation there was no workbook close() method in POI.

    Now I believe the method should be used to close the workbook and release the underlying file resources. So it is a good time to raise a bug in jxls-reader issue tracker