1
0
-1

Joget log keep warn possible unclosed DB connections. What is wrong with my scripting.


import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import javax.sql.DataSource; // add this
import org.joget.apps.app.service.AppUtil; // add this
import org.apache.commons.collections.SequencedHashMap;
import java.util.*;

public FormRowSet getIslas() {
    FormRowSet f = new FormRowSet();
        f.setMultiRow(true);

        Connection con = null;// add this
        DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");// add this
        con = ds.getConnection();// add this
    if(!con.isClosed()){
        
        String sql = "SELECT DISTINCT dir_employment.userId, dir_employment.departmentId, dir_department.id, dir_department.name,  dir_department.hod, dir_user.username, dir_user.firstName FROM dir_employment INNER JOIN dir_department ON dir_employment.departmentId=dir_department.id INNER JOIN dir_user ON dir_user.username=dir_employment.userId";
        PreparedStatement stmt = con.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        FormRow r1 = new FormRow();
        r1.put(FormUtil.PROPERTY_VALUE, "");
        r1.put(FormUtil.PROPERTY_LABEL, "-- PLEASE CHOOSE --");
        f.add(r1);
                while (rs.next()) {
            FormRow r2 = new FormRow();
            r2.put(FormUtil.PROPERTY_VALUE, rs.getString("userId"));
            r2.put(FormUtil.PROPERTY_LABEL, rs.getString("firstName"));
            r2.put(FormUtil.PROPERTY_GROUPING, rs.getString("departmentId"));
            f.add(r2);

            }
        rs.close();
        stmt.close();
        con.close();
    }
    return f;
}

return getIslas()

    CommentAdd your comment...

    1 answer

    1.  
      2
      1
      0

      Hi, you need to close your resources especially the connection within a finally block https://stackoverflow.com/questions/32816875/why-jdbc-connections-needs-to-close-in-finally-block. Otherwise there will be a leak when an exception happens.

        CommentAdd your comment...