[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [metrics-web/master] Try harder to close all database connections.



Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Wed, 10 Nov 2010 09:12:20 +0100
Subject: Try harder to close all database connections.
Commit: 722c63f4f46802084db22524c7745d53dcd8cdcb

---
 .../torproject/ernie/web/DescriptorServlet.java    |   34 +++++---------------
 src/org/torproject/ernie/web/RelayServlet.java     |   33 ++++---------------
 2 files changed, 15 insertions(+), 52 deletions(-)

diff --git a/src/org/torproject/ernie/web/DescriptorServlet.java b/src/org/torproject/ernie/web/DescriptorServlet.java
index d9166ee..cc32de6 100644
--- a/src/org/torproject/ernie/web/DescriptorServlet.java
+++ b/src/org/torproject/ernie/web/DescriptorServlet.java
@@ -123,24 +123,6 @@ public class DescriptorServlet extends HttpServlet {
     PrintWriter out = response.getWriter();
     writeHeader(out);
 
-    /* Check if we have a data source and get a database connection. */
-    Connection conn = null;
-    if (this.ds != null) {
-      try {
-        conn = this.ds.getConnection();
-      } catch (SQLException e) {
-      }
-    }
-    if (conn == null) {
-      out.println("<br/><p><font color=\"red\"><b>Warning: </b></font>"
-          + "This server doesn't have any relay descriptors available. "
-          + "If this problem persists, please "
-          + "<a href=\"mailto:tor-assistants@xxxxxxxxxxxxx\";>let us "
-          + "know</a>!</p>\n");
-      writeFooter(out);
-      return;
-    }
-
     /* Check desc-id parameter. */
     String descIdParameter = request.getParameter("desc-id");
     String descId = null;
@@ -166,6 +148,7 @@ public class DescriptorServlet extends HttpServlet {
     if (descId.length() < 40) {
       SortedSet<String> allDescIds = new TreeSet<String>();
       try {
+        Connection conn = this.ds.getConnection();
         Statement statement = conn.createStatement();
         String query = "SELECT DISTINCT descriptor FROM statusentry "
             + "WHERE descriptor LIKE '" + descId + "%'";
@@ -175,6 +158,7 @@ public class DescriptorServlet extends HttpServlet {
         }
         rs.close();
         statement.close();
+        conn.close();
       } catch (SQLException e) {
         out.println("<p><font color=\"red\"><b>Warning: </b></font>We "
             + "experienced an unknown database problem while looking up "
@@ -211,6 +195,7 @@ public class DescriptorServlet extends HttpServlet {
         extrainfo = null;
     byte[] rawDescriptor = null, rawExtrainfo = null;
     try {
+      Connection conn = this.ds.getConnection();
       Statement statement = conn.createStatement();
       String query = "SELECT descriptor, nickname, published, extrainfo, "
           + "rawdesc FROM descriptor WHERE descriptor = '" + descId
@@ -231,6 +216,7 @@ public class DescriptorServlet extends HttpServlet {
       }
       rs.close();
       statement.close();
+      conn.close();
     } catch (SQLException e) {
       out.write("<br/><p><font color=\"red\"><b>Warning: </b></font>"
           + "Internal server error when looking up descriptor. If this "
@@ -273,6 +259,7 @@ public class DescriptorServlet extends HttpServlet {
 
     /* Print out in which consensuses this descriptor is referenced. */
     try {
+      Connection conn = this.ds.getConnection();
       Statement statement = conn.createStatement();
       String query = "SELECT validafter, rawdesc FROM statusentry "
           + "WHERE descriptor = '" + descriptor + "' ORDER BY validafter "
@@ -299,6 +286,9 @@ public class DescriptorServlet extends HttpServlet {
           out.println("        <tt>" + line + "</tt><br/>");
         }
       }
+      rs.close();
+      statement.close();
+      conn.close();
     } catch (SQLException e) {
       out.println("<p><font color=\"red\"><b>Warning: </b></font>We "
           + "experienced an unknown database problem while looking up "
@@ -325,14 +315,6 @@ public class DescriptorServlet extends HttpServlet {
         + String.format("%d.%03d", searchTime / 1000, searchTime % 1000)
         + " seconds.</p>\n");
 
-    /* Close database connection. */
-    try {
-      conn.close();
-    } catch (SQLException e) {
-      this.logger.log(Level.WARNING, "Could not close database "
-          + "connection", e);
-    }
-
     /* Finish writing response. */
     writeFooter(out);
   }
diff --git a/src/org/torproject/ernie/web/RelayServlet.java b/src/org/torproject/ernie/web/RelayServlet.java
index c335353..eb9df72 100644
--- a/src/org/torproject/ernie/web/RelayServlet.java
+++ b/src/org/torproject/ernie/web/RelayServlet.java
@@ -129,24 +129,6 @@ public class RelayServlet extends HttpServlet {
     PrintWriter out = response.getWriter();
     writeHeader(out);
 
-    /* Check if we have a data source and get a database connection. */
-    Connection conn = null;
-    if (this.ds != null) {
-      try {
-        conn = this.ds.getConnection();
-      } catch (SQLException e) {
-      }
-    }
-    if (conn == null) {
-      out.println("<br/><p><font color=\"red\"><b>Warning: </b></font>"
-          + "This server doesn't have any relay descriptors available. "
-          + "If this problem persists, please "
-          + "<a href=\"mailto:tor-assistants@xxxxxxxxxxxxx\";>let us "
-          + "know</a>!</p>\n");
-      writeFooter(out);
-      return;
-    }
-
     /* Check fingerprint parameter. */
     String fingerprintParameter = request.getParameter("fingerprint");
     boolean validParameter = true;
@@ -177,6 +159,7 @@ public class RelayServlet extends HttpServlet {
     if (fingerprint.length() < 40) {
       SortedSet<String> allFingerprints = new TreeSet<String>();
       try {
+        Connection conn = this.ds.getConnection();
         Statement statement = conn.createStatement();
         String query = "SELECT DISTINCT fingerprint FROM statusentry "
             + "WHERE validafter >= '"
@@ -189,6 +172,7 @@ public class RelayServlet extends HttpServlet {
         }
         rs.close();
         statement.close();
+        conn.close();
       } catch (SQLException e) {
         out.println("<p><font color=\"red\"><b>Warning: </b></font>We "
             + "experienced an unknown database problem while looking up "
@@ -224,6 +208,7 @@ public class RelayServlet extends HttpServlet {
     boolean foundRelay = false;
     String lastDescriptor = null;
     try {
+      Connection conn = this.ds.getConnection();
       Statement statement = conn.createStatement();
       String query = "SELECT validafter, rawdesc FROM statusentry WHERE "
           + "validafter >= '"
@@ -277,6 +262,7 @@ public class RelayServlet extends HttpServlet {
       }
       rs.close();
       statement.close();
+      conn.close();
     } catch (SQLException e) {
       out.println("<p><font color=\"red\"><b>Warning: </b></font>We "
           + "experienced an unknown database problem while looking up "
@@ -304,6 +290,7 @@ public class RelayServlet extends HttpServlet {
     byte[] rawDescriptor = null, rawExtrainfo = null;
     if (lastDescriptor != null) {
       try {
+        Connection conn = this.ds.getConnection();
         Statement statement = conn.createStatement();
         query = "SELECT descriptor, nickname, published, extrainfo, "
             + "rawdesc FROM descriptor WHERE descriptor = '"
@@ -323,6 +310,8 @@ public class RelayServlet extends HttpServlet {
           }
         }
         rs.close();
+        statement.close();
+        conn.close();
       } catch (SQLException e) {
         out.write("<br/><p><font color=\"red\"><b>Warning: </b></font>"
             + "Internal server error when looking up descriptor. The "
@@ -381,14 +370,6 @@ public class RelayServlet extends HttpServlet {
         + String.format("%d.%03d", searchTime / 1000, searchTime % 1000)
         + " seconds.</p>\n");
 
-    /* Close database connection. */
-    try {
-      conn.close();
-    } catch (SQLException e) {
-      this.logger.log(Level.WARNING, "Could not close database "
-          + "connection", e);
-    }
-
     /* Finish writing response. */
     writeFooter(out);
   }
-- 
1.7.1