Wrap all close() calls in "ensure" blocks and simplify DB connection-making.
[mailshears.git] / lib / rm / plugins / agendav.rb
index 28f1eb4167a4a63bec61889ea47b97492d87f80f..46bd4073bfd459b4b2569ed1fd1d2f3a4657974f 100644 (file)
@@ -24,14 +24,15 @@ class AgendavRm
     sql_queries = ['DELETE FROM prefs WHERE username = $1;']
     sql_queries << 'DELETE FROM shared WHERE user_from = $1;'
 
-    connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
-                                @db_name, @db_user, @db_pass)
-
-    sql_queries.each do |sql_query|
-      connection.query(sql_query, [user.to_s()])
+    connection = PG::Connection.new(@db_hash)
+    begin
+      sql_queries.each do |sql_query|
+        connection.query(sql_query, [user.to_s()])
+      end
+    ensure
+      # Make sure the connection gets closed even if a query explodes.
+      connection.close()
     end
-
-    connection.close()
   end
 
 end