用于创建JasperReport后,池空连接未被释放

时间:2018-05-25 07:38:16

标签: java spring jasper-reports database-connection connection-pooling

我们已经在prod中使用了一个spring boot web应用程序。 最近,该公司要求将一些EOD对帐报告作为不可编辑的PDF格式生成。

我们编译了一些Jasper报告,并使用如下代码开始生成报告:

private void generatePdfReport(final Report report) throws SQLException, JRException {
    final JasperReport jasperReport = JASPER_REPORT_MAP.get(report);
    if (jasperReport == null) {
        throw new UnsupportedOperationException("The report is not supported: " + report.getName());
    } else {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource.getConnection());
            final JRPdfExporter exporter = new JRPdfExporter();
            exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(report.getFormattedFilename() + Report.EXT_PDF));
            exporter.setConfiguration(reportConfig);
            exporter.setConfiguration(exportConfig);
            exporter.exportReport();
            log.info("PDF Report ({}) exported successfully!", report.getName());
        } finally {
            if (connection != null) {
                log.info("about to close db connection");
                connection.close();
                connection = null;
            }
        }
    }
}

报告运行良好但在生成十个报告后,我们开始从Hitaki数据源获取超时异常,抱怨它无法在30秒内获得连接。

Hitaki数据源的默认连接池大小为10,并且在Hitaki类中放置了一些断点,我们可以看到所有十个连接都标记为IN_USE。将数据源更改为commons-dbcp没有太大变化。此次没有超时,但在生成8个报告后,处理无限期地被阻止。 Eight是dbcp数据源的默认池大小。

总结一下,这不是数据源问题,我们在jasper jar中添加了一些断点,并注意到创建的结果集和预处理语句都已正确关闭。

我们的Hitaki / DBCP数据源由spring TransactionAwareDataSourceProxy实例包装。取出那个包装没有改变任何东西。

最后,我用一个非常基本的数据源(显然不适合生产)替换了数据源,并且一切正常。请参阅下面的代码,显示我们尝试过的评论:

    @Bean
    public DataSource dataSource() {
        final String url = env.getProperty("database.url");
        final String userName = env.getProperty("gmm.schema");
        log.info("Creating DataSource for {}@{}", userName, url);
        // final HikariDataSource dataSource = new HikariDataSource();
        // final BasicDataSource dataSource = new BasicDataSource();
        final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(env.getProperty("gmm.password"));
        // dataSource.setDriverClassName(env.getProperty("database.driver"));
       dataSource.setDriverClass(oracle.jdbc.driver.OracleDriver.class);
        return dataSource;
        // return new TransactionAwareDataSourceProxy(dataSource);
    }

现在我的问题:

导致此类行为的原因以及我应该如何进一步修复或调查。如果我们注释掉报告作业,则没有连接问题,但另一方面,我没有看到使用非常基本的数据源如何解决这个问题。

成为Jasper Reports的新手我只是希望我没有正确使用它。

提前谢谢你。

3 个答案:

答案 0 :(得分:1)

对我来说,try-with-resources 有效:

JasperPrint jasperPrint;
    try (Connection connection = dataSource.getConnection()) {
      jasperPrint = JasperFillManager.fillReport(report, params, connection);
    }

答案 1 :(得分:0)

尝试将您创建的连接对象传递给fillReport方法。

connection = dataSource.getConnection();
final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource.getConnection());

connection = dataSource.getConnection();
        final JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, connection);

干杯

答案 2 :(得分:0)

对我来说,我使用了DataSourceUtils:

connection = DataSourceUtils.doGetConnection(dataSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(report, params, connection);

...

DataSourceUtils.releaseConnection(connection, dataSource);

在每次呼叫后使用简单连接,我有-1个空闲连接池。