Uploaded image for project: 'Sakai'
  1. Sakai
  2. SAK-19330

Generic Jira: Database resources are not correctly released [Static Code Review]

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: RESOLVED
    • Priority: Major
    • Resolution: Won't Fix
    • Affects Version/s: 2.8.0
    • Fix Version/s: None
    • Labels:
      None
    • Previous Issue Keys:
      SAK-13136, SRCH-4

      Description

      9 locations potentially fail to release every database resource they have aqcuired.
      See http://qa1-nl.sakaiproject.org/codereview/bug_dashboard/findbugs_database.html

      For example (from FixAnswerScore:238)

      Connection conn = null;
      PreparedStatement stmt = null;
      ResultSet rs = null;
      try {
      conn = DriverManager.getConnection(url);
      stmt = conn.prepareStatement(query);
      rs = stmt.executeQuery();
      }
      catch (Exception e) {
      e.printStackTrace();
      }
      finally{
      try {
      if (rs !=null)

      { rs.close(); }
      if (stmt !=null){ stmt.close(); }
      if (conn !=null){ conn.close(); }
      } catch (Exception e1) { e1.printStackTrace(); }
      }

      Obviously some thought has gone into this code, but it can still go wrong and not release every resource it has aqcuired.
      What happens is that in the finally block each item is checked against not being null so NullPointerExceptions cannot occur. But what if the ResultSet rs throws an IOException; it gets caught as Exception e1 and the close for PreparedStatement stmt and Connection conn are never executed which can result in a leaking resource.

      One way of preventing this is by try catching every close for IOException. I realise this is more verbose but it prevents the database holding on to resources that will not be needed again by your code.

      Connection conn = null;
      PreparedStatement stmt = null;
      ResultSet rs = null;
      try {
      conn = DriverManager.getConnection(url);
      stmt = conn.prepareStatement(query);
      rs = stmt.executeQuery();
      }
      catch (Exception e) {
      e.printStackTrace();
      }
      finally{
      if (rs !=null){
      try { rs.close(); }

      catch (Exception ioe)

      { e1.printStackTrace(); }
      }
      if (stmt !=null){
      try { stmt.close(); } catch (Exception ioe) { e1.printStackTrace(); }

      }
      if (conn !=null){
      try

      { conn.close(); }

      catch (Exception ioe)

      { e1.printStackTrace(); }

      }
      }

        Gliffy Diagrams

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  osp-jira OSP Jira Team (Inactive)
                  Reporter:
                  gnooteb2 Gilgamesh Nootebos (Inactive)
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  2 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:

                    Git Source Code