saran1097 Posted March 22, 2012 Share Posted March 22, 2012 Hi,I have a main report with two sub-reports. The main report and sub-report2 are fed by JavaBeans data source and sub-report1 has its data source configured to a generated CSV file (the file name of the CSV data source is passed as a parameter; and this CSV file is a temporary file created by the Java module triggering the japser report).Everything is okay, the report gets generated - BUT after the report generation when my application tries to delete the temporary CSV file created for sub-report1, it fails to do so because the handle has not been released yet.This is the flow of my program:1) Create ArrayList of elements for Main report and sub-report22) Create a temporary CSV file containing records for sub-report1 (Why is this required? our requirement is to support millions of records - and holding millions of data objects in memory kills the JVM even with heap size of 1.5GB - so we write the data object to a temporary file and pass this file as data source for the Jasper report - copy catted the JRSwapFile concept to eliminate memory issues) Note: at this point we close the BufferedWriter and the file does not have any handles.3) Pass the temporary CSV file’s path as parameter and the ArrayList of objects for Main and Sub-report2 to Jasper4) Fill the report (now the CSV file has an handle)5) Deliver the report6) Try to delete the temporary file – this is where f.delete() fails because the file handle has not been released.Sub-report1’s data source is configured as<dataSourceExpression><![CDATA[new com.reports.datasource.CsvDataSource(new java.io.File($P{SUBREPORT1_DATA_SOURCE}), true)]]></dataSourceExpression>Based on the suggestions given by Peter in this post: http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=15779&page=2 Please let me know how I can force the Jasper Fill process to release the file handle of the CSV data source once it is done filling the report. Thanks in advance for your time and help,Saran Other Details: JasperReports v 4.1.1Struts, Java 1.5 application running in Windows/Apache tomcat and Unix/WebSphere application servers Post Edited by saran1097 at 03/22/2012 17:21Post Edited by saran1097 at 03/22/2012 20:22 Link to comment Share on other sites More sharing options...
saran1097 Posted March 22, 2012 Author Share Posted March 22, 2012 SOLVED!Posting the solution here so that might be useful for others.I changed my report parameter to <parameter name="SUBREPORT1_DATA_SOURCE" class="net.sf.jasperreports.engine.data.JRCsvDataSource" isForPrompting="false"/>and changed the datasource for subreport as<dataSourceExpression><![CDATA[$P{SUBREPORT1_DATA_SOURCE}]]></dataSourceExpression>Instead of using the custom CsvDataSource as specified in the other post, I now pass the JRCsvDataSource from my java code as a report parameter, and then finally close and delete the file.Code snippet here: Thanks,SaranCode:JRCsvDataSource subreport = new JRCsvDataSource(csvFileName);subreport.setUseFirstRowAsHeader(true);reportParms.addParameter("SUBREPORT1_DATA_SOURCE", subreport);:::finally{ if(subreport!=null) { subreport.close(); File f=new File(csvFileName); if(f.exists() && f.isFile()){ boolean success = f.delete(); if (!success) { log.error("cleanUpLocalCSVFiles - could not delete swap file:"+csvFileName+" - Manual cleanup needed."); } else log.info("cleanUpLocalCSVFiles - DELETED swap file:"+csvFileName); }} Post Edited by saran1097 at 03/23/2012 22:55 Link to comment Share on other sites More sharing options...
edgar_barbosa Posted March 18, 2016 Share Posted March 18, 2016 Another possibility is to force the Stream close: java.io.InputStream iStream = JRLoader.getLocationInputStream("file.csv");JRCsvDataSource ds = new JRCsvDataSource(iStream);ds.setUseFirstRowAsHeader(true);ds.setDateFormat(new SimpleDateFormat("dd-MM-yyyy"));ds.setRecordDelimiter("n"); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), ds); JasperExportManager.exportReportToPdfFile(jasperPrint, "file.pdf");byte[] pdfBytes = JasperExportManager.exportReportToPdf(jasperPrint);iStream.close();ds.close(); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now