Connection leak while generating jasper report

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

javaenthu gravatar image javaenthu
141 2

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!

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 :

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"/>
<button label="Report!" onClick='showReport()'/>
<jasperreport id="report" height="360px" />

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.setDatasource(new CustomDataSource());
report.setType((String) format.getSelectedItem().getValue());

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

Any workaround this?

Thanks in advance.

link publish delete flag offensive edit

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

Jasonec gravatar image Jasonec

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 {

    public Object getExtraCtrl() {
        return new ExtraCtrl();

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

    protected class ExtraCtrl extends Jasperreport.ExtraCtrl {

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

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

<?xml version="1.0" encoding="windows-1250"?>

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




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