@@ -76,6 +76,8 @@ public class EpipulseCsvExportOrchestrator {
7676 public void orchestrateExport (String uuid , ExportFunction exportFunction , CsvExportStrategy csvStrategy ) {
7777
7878 CSVWriter writer = null ;
79+ FileOutputStream fos = null ;
80+ OutputStreamWriter osw = null ;
7981 EpipulseExport epipulseExport = null ;
8082 EpipulseExportStatus exportStatus = EpipulseExportStatus .FAILED ;
8183 boolean shouldUpdateStatus = false ;
@@ -117,10 +119,10 @@ public void orchestrateExport(String uuid, ExportFunction exportFunction, CsvExp
117119 EpipulseDiseaseExportResult exportResult = exportFunction .execute (exportDto , serverCountryCode , serverCountryName );
118120 totalRecords = exportResult .getExportEntryList ().size ();
119121
120- // Setup CSV writer
121- writer = CSVUtils . createCSVWriter (
122- new OutputStreamWriter (new FileOutputStream ( exportFilePath ) , StandardCharsets .UTF_8 ),
123- configFacadeEjb .getCsvSeparator ());
122+ // Setup CSV writer with explicit stream management
123+ fos = new FileOutputStream ( exportFilePath );
124+ osw = new OutputStreamWriter (fos , StandardCharsets .UTF_8 );
125+ writer = CSVUtils . createCSVWriter ( osw , configFacadeEjb .getCsvSeparator ());
124126
125127 // Build column names using strategy
126128 List <String > columnNames = csvStrategy .buildColumnNames (exportResult );
@@ -140,14 +142,28 @@ public void orchestrateExport(String uuid, ExportFunction exportFunction, CsvExp
140142 exportStatus = EpipulseExportStatus .FAILED ;
141143 logger .error ("Error during export with uuid " + uuid + ": " + e .getMessage (), e );
142144 } finally {
143- // Close writer
145+ // Close resources in reverse order
144146 if (writer != null ) {
145147 try {
146148 writer .close ();
147149 } catch (Exception e ) {
148150 logger .error ("CRITICAL: Failed to close CSVWriter for uuid " + uuid + ": " + e .getMessage (), e );
149151 }
150152 }
153+ if (osw != null ) {
154+ try {
155+ osw .close ();
156+ } catch (Exception e ) {
157+ logger .error ("CRITICAL: Failed to close OutputStreamWriter for uuid " + uuid + ": " + e .getMessage (), e );
158+ }
159+ }
160+ if (fos != null ) {
161+ try {
162+ fos .close ();
163+ } catch (Exception e ) {
164+ logger .error ("CRITICAL: Failed to close FileOutputStream for uuid " + uuid + ": " + e .getMessage (), e );
165+ }
166+ }
151167
152168 // Calculate file size after writer is closed
153169 if (exportFilePath != null && exportStatus == EpipulseExportStatus .COMPLETED ) {
0 commit comments