0

Connection leak while generating jasper report

asked 2012-07-12 12:52:51 +0800

javaenthu gravatar image javaenthu
141 2

Hi,
From my composer code I am setting the dbConnection to the jasper report.
Report is getting generated nicely but I see ZK is not closing the connection.
I am using zk version 5.0.9 , zkex : 5.0.5 , jasper reports jar version : 4.5.1 and poi version 3.7, iText : 2.1.7

Thanks in advance!
-JE

delete flag offensive retag edit

2 Replies

Sort by » oldest newest

answered 2012-07-23 10:48:14 +0800

javaenthu gravatar image javaenthu
141 2

updated 2012-07-23 10:58:53 +0800

I am taking the reference from ZK Jasper Demo.

precisely code is something like this :

<zk>
Choice the file type : <listbox id="format" mold="select" onSelect="showReport()" >
<listitem label="PDF" value="pdf" selected="true" />
<listitem label="XML" value="xml" />
<listitem label="HTML" value="html" />
<listitem label="Word (RTF)" value="rtf" />
<listitem label="Excel" value="xls" />
<listitem label="Excel (JXL)" value="jxl" />
<listitem label="CSV" value="csv" />
<listitem label="OpenOffice (ODT)" value="odt" unless="false"/>
</listbox>
<button label="Report!" onClick='showReport()'/>
<jasperreport id="report" height="360px" />

<zscript>
import org.zkoss.zksandbox.CustomDataSource;

void showReport() {
//Preparing parameters
Map parameters = new HashMap();
parameters.put("ReportTitle", "Address Report");
parameters.put("DataFile", "CustomDataSource from java");

report.setSrc("/data/jasperreport.jasper");
report.setParameters(parameters);
report.setDatasource(new CustomDataSource());
report.setType((String) format.getSelectedItem().getValue());
}
</zscript>
</zk>

Only thing is I have moved connection related things to composer.

Any workaround this?

Thanks in advance.
-JE.

link publish delete flag offensive edit

answered 2020-12-19 10:03:02 +0800

Jasonec gravatar image Jasonec
1

I've found a solution creating a custom component based on ZK Jasperreport component which open sql Connection only when needed and close it inmmediately after, avoiding connection leaks. You have to create the component class (which I named JasperreportUtil) and define it on lang-addon.xml file. After that, you have to change your Composer (or zscript) to use our custom component class instead of ZK Jasperreport and avoid call the method setDataConnection because now the connection will be set internally. Finally, you have to change the <jasperreport> tag name in zul files by the new one (I named <jasperreportutil>).

Here is the source code of JasperreportUtil component class:

import java.sql.Connection;
import org.zkoss.util.media.Media;
import org.zkoss.zkex.zul.Jasperreport;

/**
 * Component class based on {@link Jasperreport} which avoids connection leaks.
 * @author Jason Jijón
 */
public class JasperreportUtil extends Jasperreport {

    @Override
    public Object getExtraCtrl() {
        return new ExtraCtrl();
    }

    @Override
    @Deprecated
    public void setDataConnection(Connection connection) {
        throw new UnsupportedOperationException("Don't use this method because connection is assigned internally.");
    }

    protected class ExtraCtrl extends Jasperreport.ExtraCtrl {

        @Override
        public Media getMedia(String pathInfo) {
            try (Connection con = YourConnectionProvider.getConnection()) {
                JasperreportUtil.super.setDataConnection(con);
                return super.getMedia(pathInfo);
            } catch(SQLException e) {                    
                throw new RuntimeException(e);
            } finally {
                JasperreportUtil.super.setDataConnection(null);
            }
        }    
    }
}

The lang-addon.xml file could be the following:

<?xml version="1.0" encoding="windows-1250"?>
<language-addon>
    <addon-name>example</addon-name>
    <language-name>xul/html</language-name>

    <component>
        <component-name>jasperreportutil</component-name>
        <component-class>com.example.components.JasperreportUtil</component-class>
        <widget-class>zkex.utl.Jasperreport</widget-class>
        <mold>
            <mold-name>default</mold-name>
            <mold-uri>mold/jasperreport.js</mold-uri>
        </mold>
    </component>
</language-addon>
link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-07-12 12:52:51 +0800

Seen: 144 times

Last updated: Dec 19 '20

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More