fixes issue 36697: connection leak when using Utility.showImage in a subreport
authorCarlos Aristu <carlos.aristu@openbravo.com>
Thu, 24 Aug 2017 12:12:26 +0200
changeset 32582 80ab9332dfc5
parent 32581 966241a1d691
child 32583 120b7e711081
fixes issue 36697: connection leak when using Utility.showImage in a subreport

Ensure that the showImage method, which is part of the API used by the reports to retrieve an image from the database, closes the DAL transaction after using it so that the connection is returned back to the pool. Thus, we avoid connection leaks when it is used in a subreport.
src/org/openbravo/erpCommon/utility/Utility.java
--- a/src/org/openbravo/erpCommon/utility/Utility.java	Thu Aug 24 11:28:37 2017 +0200
+++ b/src/org/openbravo/erpCommon/utility/Utility.java	Thu Aug 24 12:12:26 2017 +0200
@@ -1991,19 +1991,31 @@
   }
 
   /**
-   * Provides the image as a byte array. These images are stored in the table AD_IMAGES as a BLOB
+   * Provides the image as a byte array. These images are stored in the table AD_IMAGE as a BLOB
    * field.
    * 
    * @param id
    *          The id of the image to display
    * @return The image requested
-   * @see #getImage(String)
    */
   public static byte[] getImage(String id) {
+    return getImage(id, false);
+  }
 
+  /**
+   * Provides the image as a byte array. These images are stored in the table AD_IMAGE as a BLOB
+   * field.
+   *
+   * @param id
+   *          The id of the image to display
+   * @param doCommit
+   *          A flag to force the commit of the DAL connection after retrieve the image
+   * @return The image requested
+   */
+  private static byte[] getImage(String id, boolean doCommit) {
     byte[] imageByte;
     try {
-      Image img = getImageObject(id);
+      Image img = getImageObject(id, doCommit);
       if (img == null) {
         imageByte = getBlankImage();
       } else {
@@ -2023,7 +2035,7 @@
   }
 
   /**
-   * Provides the image as an image object. These images are stored in the table AD_IMAGES as a BLOB
+   * Provides the image as an image object. These images are stored in the table AD_IMAGE as a BLOB
    * field.
    * 
    * @param id
@@ -2032,6 +2044,20 @@
    * @see #getImage(String)
    */
   public static Image getImageObject(String id) {
+    return getImageObject(id, false);
+  }
+
+  /**
+   * Provides the image as an image object. These images are stored in the table AD_IMAGE as a BLOB
+   * field.
+   *
+   * @param id
+   *          The id of the image to display
+   * @param doCommit
+   *          A flag to force the commit of the DAL connection after retrieve the image
+   * @return The image requested
+   */
+  private static Image getImageObject(String id, boolean doCommit) {
     Image img = null;
     OBContext.setAdminMode();
     try {
@@ -2040,6 +2066,9 @@
       log4j.error("Could not load image from database: " + id, e);
     } finally {
       OBContext.restorePreviousMode();
+      if (doCommit) {
+        OBDal.getInstance().commitAndClose();
+      }
     }
     return img;
   }
@@ -2054,7 +2083,10 @@
    * @see #getImage(String)
    */
   public static BufferedImage showImage(String id) throws IOException {
-    return ImageIO.read(new ByteArrayInputStream(getImage(id)));
+    // Use getImage(id, true) to close the DAL connection once the image has been retrieved.
+    // This is required to avoid connection leaks when invoking this method from a sub-report.
+    // This is needed until issue https://issues.openbravo.com/view.php?id=30182 is fixed.
+    return ImageIO.read(new ByteArrayInputStream(getImage(id, true)));
   }
 
   /**