[OSM-dev] [PATCH] Persistant database connection

James Mastros james at mastros.biz
Sun Jul 9 20:20:00 BST 2006


Currently, the core of OSM is a bit silly: Every time it wants to execute
some SQL, it connects to the database, runs the sql, gets the entire result
set, and then disconnects from the database.  There's a number of ways in
which that's silly, but the simplest one is that there's no reason to keep
connecting and disconnecting -- connect when you first need the connection,
and disconnect when you're all done.

The attached patch touches only a very small amount of code, and will do
just that.  There's still plenty of sillyness about, but this will at least
pick this piece of low-hanging fruit.

(There are two versions attatched; they differ only in idiom.  Pick
whichever you like better.)

     -=- James Mastros
-------------- next part --------------
Index: www.openstreetmap.org/ruby/api/osm/dao.rb
===================================================================
--- www.openstreetmap.org/ruby/api/osm/dao.rb	(revision 1117)
+++ www.openstreetmap.org/ruby/api/osm/dao.rb	(working copy)
@@ -163,7 +163,8 @@
 
     def get_connection
       begin
-        return Mysql.real_connect($DBSERVER, $USERNAME, $PASSWORD, $DATABASE)
+        @connection ||= Mysql.real_connect($DBSERVER, $USERNAME, $PASSWORD, $DATABASE)
+        return @connection
       rescue MysqlError => e
         mysql_error(e)
       end
@@ -242,8 +243,6 @@
         if res.nil? then return true else return res end
       rescue MysqlError =>ex
         mysql_error(ex)
-      ensure
-        dbh.close unless dbh.nil?
       end
       nil
     end
-------------- next part --------------
Index: www.openstreetmap.org/ruby/api/osm/dao.rb
===================================================================
--- www.openstreetmap.org/ruby/api/osm/dao.rb	(revision 1117)
+++ www.openstreetmap.org/ruby/api/osm/dao.rb	(working copy)
@@ -163,7 +163,11 @@
 
     def get_connection
       begin
-        return Mysql.real_connect($DBSERVER, $USERNAME, $PASSWORD, $DATABASE)
+        if @connection
+          return @connection
+        end
+        @connection = Mysql.real_connect($DBSERVER, $USERNAME, $PASSWORD, $DATABASE)
+        return @connection
       rescue MysqlError => e
         mysql_error(e)
       end
@@ -242,8 +246,6 @@
         if res.nil? then return true else return res end
       rescue MysqlError =>ex
         mysql_error(ex)
-      ensure
-        dbh.close unless dbh.nil?
       end
       nil
     end


More information about the dev mailing list