Bugs: Browse | Submit New | Admin

[#29633] Deletes cause too many handles to be consumed and not freed

Date:
2012-08-28 05:02
Priority:
3
Submitted By:
Beau Fabry (bfabry)
Assigned To:
Praveen Devarao (praveend)
Category:
Adapter
State:
Open
Summary:
Deletes cause too many handles to be consumed and not freed

Detailed description
The update/delete method prepares a statement which it never frees. Below is an updated method which corrects this behaviour.
This problem is observed with v2.5.10


/lib/active_record/connection_adapters/ibm_db_adapter.rb
    module ActiveRecord
      module ConnectionAdapters
        class IBMAdapter
          def update(arel, name = nil, binds = [])
            sql = to_sql(arel)

            # Make sure the WHERE clause handles NULL's correctly
            sqlarray = sql.split(/\s*WHERE\s*/)
            size = sqlarray.size
            if size > 1
              sql = sqlarray[0] + " WHERE "
              if size > 2
                1.upto size-2 do |index|
                  sqlarray[index].gsub!( /(=\s*NULL|IN\s*\(NULL\))/i, " IS NULL" ) unless
sqlarray[index].nil?
                  sql = sql + sqlarray[index] + " WHERE "
                end
              end
              sqlarray[size-1].gsub!( /(=\s*NULL|IN\s*\(NULL\))/i, " IS NULL" ) unless sqlarray[size-1].nil?
              sql = sql + sqlarray[size-1]
            end

            clear_query_cache if defined? clear_query_cache

            if binds.nil? || binds.empty?
              update_direct(sql, name)
            else
              if stmt = exec_query(sql,name,binds)
                num = IBM_DB.num_rows(stmt)
                IBM_DB.free_stmt(stmt) # This is the important line added
                num
              end
            end
          end
        end
      end
    end

Add A Comment: Notepad

Please login


Followup

Message
Date: 2012-09-02 23:40
Sender: Beau Fabry

No problems from me, that's probably safer.
Date: 2012-09-02 15:46
Sender: Praveen Devarao

Hi Beau,

Thanks for your time on looking into this and the fix.

The fix will be rolled out as part of the next release of the
gem.

I will prefer to handle the free of statement in ensure block
as done in other methods. What do you say?

>>
if binds.nil? || binds.empty?
              update_direct(sql, name)
            else
              begin
                if stmt = exec_query(sql,name,binds)
                  IBM_DB.num_rows(stmt)
                end
              ensure
                IBM_DB.free_stmt(stmt) if(stmt)
              end
<<

Let me know if you see any concerns on handling it in ensure
block. If none I will make changes accordingly in the next
release.

Thanks

Praveen 

Attached Files:

Name Description Download
No Files Currently Attached

Changes:

Field Old Value Date By
assigned_toalexp2012-09-02 15:46praveend