7fe80d37afd296f8db0058f3e444737ff88ff80e
markd
  Mon Jul 26 23:50:19 2010 -0700
address problem of hgTables consuming all available sockets before TIME_WAIT period by using the connection cache. Have hgTable log connection usage information to help ensure this is fixed
diff --git src/hg/lib/hdb.c src/hg/lib/hdb.c
index 23a765a..82b7827 100644
--- src/hg/lib/hdb.c
+++ src/hg/lib/hdb.c
@@ -273,9 +273,9 @@
 
 if (dbsChecked)
     {
-    void *hashDb = hashFindVal(dbsChecked, database);
-    if (hashDb)
-	return(hashIntVal(dbsChecked, database));
+    struct hashEl *hel = hashLookup(dbsChecked, database);
+    if (hel != NULL)
+	return ptToInt(hel->val);
     }
 else
     dbsChecked = newHash(0);
@@ -283,11 +283,18 @@
 struct sqlConnection *conn = hConnectCentral();
 char buf[128];
 char query[256];
-boolean res = FALSE;
 char *escaped = sqlEscapeString(database);
 safef(query, sizeof(query), "select name from dbDb where name = '%s'", escaped);
 freez(&escaped);
-res = (sqlQuickQuery(conn, query, buf, sizeof(buf)) != NULL) && sqlDatabaseExists(database);
+boolean res = (sqlQuickQuery(conn, query, buf, sizeof(buf)) != NULL);
+if (res)
+    {
+    // this is done instead of sqlDatabaseExists() since it uses the cache,
+    // which will recycle free connections for new databases
+    struct sqlConnection *conn2 = hAllocConnMaybe(database);
+    res = (conn2 != NULL);
+    hFreeConn(&conn2);
+    }
 hDisconnectCentral(&conn);
 hashAddInt(dbsChecked, database, res);
 return res;
@@ -481,6 +488,15 @@
 return sqlConnCacheAlloc(hdbCc, db);
 }
 
+struct sqlConnection *hAllocConnMaybe(char *db)
+/* Get free connection if possible. If not allocate a new one. Return
+ * NULL if db doesn't exist or can't be connected to. */
+{
+if (hdbCc == NULL)
+    hdbCc = sqlConnCacheNew();
+return sqlConnCacheMayAlloc(hdbCc, db);
+}
+
 struct sqlConnection *hAllocConnProfile(char *profileName, char *db)
 /* Get free connection, specifying a profile and/or a database. If none
  * is available, allocate a new one. */
@@ -3899,11 +3915,11 @@
 /* Get list of active databases, in theDb's clade if theDb is not NULL.
  * Dispose of this with dbDbFreeList. */
 {
-struct sqlConnection *conn = hConnectCentral();
+char *theClade = theDb ? hClade(hGenome(theDb)) : NULL;
+struct sqlConnection *conn = hConnectCentral(); // after hClade, since it access hgcentral too
 struct sqlResult *sr = NULL;
 char **row;
 struct dbDb *dbList = NULL, *db;
-char *theClade = theDb ? hClade(hGenome(theDb)) : NULL;
 
 /* Scan through dbDb table, loading into list */
 if (theClade != NULL)
@@ -4532,6 +4548,7 @@
 	grpSuperimpose(&grps, &oneTable);
     else if (!grps)
 	grps = oneTable;
+    hFreeConn(&conn);
     }
 return grps;
 }