From 4200b097e5ca99b5a221808f1ed344a21bf4d18a Mon Sep 17 00:00:00 2001 From: Jason Skomorowski Date: Fri, 8 Aug 2025 20:54:04 -0400 Subject: [PATCH 1/2] Shorter use of db connection for reports --- .../mod/rs/ReportController.groovy | 3 +++ .../rs/reporting/JasperReportService.groovy | 22 ++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/service/grails-app/controllers/mod/rs/ReportController.groovy b/service/grails-app/controllers/mod/rs/ReportController.groovy index a5251eb43..7cf316aac 100644 --- a/service/grails-app/controllers/mod/rs/ReportController.groovy +++ b/service/grails-app/controllers/mod/rs/ReportController.groovy @@ -12,6 +12,7 @@ import com.k_int.okapi.OkapiTenantAwareController; import grails.converters.JSON; import grails.gorm.multitenancy.CurrentTenant; +import grails.gorm.transactions.NotTransactional; import groovy.util.logging.Slf4j; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; @@ -171,6 +172,7 @@ class ReportController extends OkapiTenantAwareController { dataType = "string" ) ]) + @NotTransactional def generatePicklist() { ContextLogging.startTime(); ContextLogging.setValue(ContextLogging.FIELD_RESOURCE, RESOURCE_REPORT); @@ -235,6 +237,7 @@ class ReportController extends OkapiTenantAwareController { dataType = "string" ) ]) + @NotTransactional def execute() { ContextLogging.startTime(); ContextLogging.setValue(ContextLogging.FIELD_RESOURCE, RESOURCE_REPORT); diff --git a/service/grails-app/services/org/olf/rs/reporting/JasperReportService.groovy b/service/grails-app/services/org/olf/rs/reporting/JasperReportService.groovy index 273574041..3a258c2d9 100644 --- a/service/grails-app/services/org/olf/rs/reporting/JasperReportService.groovy +++ b/service/grails-app/services/org/olf/rs/reporting/JasperReportService.groovy @@ -99,7 +99,8 @@ public class JasperReportService { // If you are having issues with fonts, take a look at // https://community.jaspersoft.com/wiki/custom-font-font-extension Connection connection = dataSource.getConnection(); - try{ + JasperPrint jasperPrint; + try { Map parameters = new HashMap(); parameters.put(PARAMETER_IDS, idsForReport); parameters.put(PARAMETER_SCHEMA, schema); @@ -110,15 +111,10 @@ public class JasperReportService { JasperReport jasperReport = getReport(fileDefinition, fallbackReportResource); // Execute the report - JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection); - - // Now output the report - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); - result = new ByteArrayInputStream(outputStream.toByteArray()); - outputStream.close(); + jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection); } catch(Exception e) { log.error("Exception thrown while generating a report", e); + return result; } finally { // Not forgetting to close the connection connection.close(); @@ -129,6 +125,16 @@ public class JasperReportService { } } + try { + // Now output the report + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); + result = new ByteArrayInputStream(outputStream.toByteArray()); + outputStream.close(); + } catch(Exception e) { + log.error("Exception thrown while generating a report PDF", e); + } + // Return the result to the caller return(result); } From 9e78fb3e0121dc6338780b7ed02295539019fd01 Mon Sep 17 00:00:00 2001 From: Jason Skomorowski Date: Mon, 11 Aug 2025 16:32:50 -0400 Subject: [PATCH 2/2] Outputstream needs to be closed either way. --- .../org/olf/rs/reporting/JasperReportService.groovy | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/service/grails-app/services/org/olf/rs/reporting/JasperReportService.groovy b/service/grails-app/services/org/olf/rs/reporting/JasperReportService.groovy index 3a258c2d9..3123efa7b 100644 --- a/service/grails-app/services/org/olf/rs/reporting/JasperReportService.groovy +++ b/service/grails-app/services/org/olf/rs/reporting/JasperReportService.groovy @@ -95,6 +95,7 @@ public class JasperReportService { String fallbackReportResource = null ) { InputStream result = null; + ByteArrayOutputStream outputStream = null; // If you are having issues with fonts, take a look at // https://community.jaspersoft.com/wiki/custom-font-font-extension @@ -127,12 +128,13 @@ public class JasperReportService { try { // Now output the report - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + outputStream = new ByteArrayOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); result = new ByteArrayInputStream(outputStream.toByteArray()); - outputStream.close(); } catch(Exception e) { log.error("Exception thrown while generating a report PDF", e); + } finally { + outputStream.close(); } // Return the result to the caller