Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is disposing SXSSFWorkbook necessary when used in try with resource

Below is the sample code snippet to create SXSSFWorkbook:

try(SXSSFWorkbook wb = new SXSSFWorkbook()) {
    //...
} finally {
    wb.dispose(); //wb not accessible over here, so can't use try with resource
}

Here problem is that if I use try with resource then can't dispose() SXSSFWorkbook in finally, as variable wb won't be accessible in finally block.

I wanted know that is disposing of workbook necessary to delete temporary files or since SXSSFWorkbook is AutoCloseable, try with resource will take care of it.

like image 564
eatSleepCode Avatar asked Mar 07 '23 05:03

eatSleepCode


1 Answers

Not sure whether someone of the apache poi programmers will answering this. But apache poi is open source. So every programmer can answering this itself by looking at the code.

State May 2018, apache poiversion 3.17.

SXSSFWorkbook.java:

public class SXSSFWorkbook implements Workbook

So why can this be a resource for using in try with resource? Because

Workbook.java:

public interface Workbook extends Closeable, Iterable<Sheet>

So org.apache.poi.ss.usermodel.Workbook extends java.io.Closeable and so classes which implements this must providing a method close.

SXSSFWorkbook.close

As you see, the single SheetDataWriters will be closed and then the internally XSSFWorkbook _wb will be closed.

SheetDataWriter.close

SheetDataWriter.close only flushes and closes the Writer _out.

So no, nowhere the dispose is called while auto closing until now (May 2018) in apache poiversion 3.17

And only SheetDataWriter.dispose will deleting the TempFile _fd created for each sheet.

like image 55
Axel Richter Avatar answered Apr 25 '23 09:04

Axel Richter