1
0
-1

Hi, my joget log keep warn possible db unclosed. Here is my script. Is there any wrong with my script?


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

public FormRowSet getData() {

    //-----------------------------------------------------------------------------------
    //In this part of code, it trying to load the original data from form data table.

    FormRowSet results = null;

    //------------------------------------------------------------------------------------

    //------------------------------------------------------------------------------------
    //In this second part of code, it will load the data from external source by using
    //JDBC. It will run only when the first part of code fail to retrieve data from
    //form data table. This example use dir_user table of Joget as external source.

   
   
    if (results == null) {
        results = new FormRowSet();

        Connection con = null;
        try {

        //Connection con = null;// add this
        DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");// add this
        con = ds.getConnection();// add this

            if(!con.isClosed()){
                String pId = "#currentUser.username#";
                String sql = "SELECT c_alamat1, c_alamat2, c_daerah, c_negeri, c_postcode, c_email FROM app_fd_user_registration WHERE c_username=?";
                PreparedStatement stmt = con.prepareStatement(sql);
                stmt.setString(1, pId);

                ResultSet rs = stmt.executeQuery();
                while (rs.next()) {
                    FormRow row = new FormRow();
                    row.put("pemohon_alamat1", (rs.getString(1) != null)?rs.getString(1):"");
                row.put("pemohon_alamat2", (rs.getString(2) != null)?rs.getString(2):"");
                            row.put("pemohon_bandar", (rs.getString(3) != null)?rs.getString(3):"");
                row.put("pemohon_negeri", (rs.getString(4) != null)?rs.getString(4):""); 
                            row.put("pemohon_poskod", (rs.getString(5) != null)?rs.getString(5):""); 
                        
                    results.add(row);
                }
            }

        } catch(Exception ex) {
            System.err.println("Exception: " + ex.getMessage());
        } finally {
            try {
                if(con != null)
                    con.close();
            } catch(SQLException e) {}
        }
    }
    //------------------------------------------------------------------------------------

    return results;
}

return getData();

  1. Ian

    were there any errors prior to the warning? if so, what did it say?

CommentAdd your comment...

2 answers

  1.  
    1
    0
    -1

    Hi, the code looks ok since the database connection appears to be closed within a finally block. Perhaps the connection leak is from another part of the app, or in a different app altogether. 

      CommentAdd your comment...
    1.  
      1
      0
      -1

      thats cool

        CommentAdd your comment...