Project

General

Profile

Feature #17460 » mysql5.7_deadlocks_fix.patch

Jean-Philippe Lang, 2015-03-16 21:20

View differences:

lib/redmine/nested_set/issue_nested_set.rb (working copy)
149 149
      end
150 150

  
151 151
      def lock_nested_set
152
        if self.class.connection.adapter_name =~ /sqlserver/i
153
          lock = "WITH (ROWLOCK HOLDLOCK UPDLOCK)"
154
          # Custom lock for SQLServer
152
        if Redmine::Database.postgresql?
153
          sets_to_lock = [id, parent_id].compact
154
          self.class.reorder(:id).where("root_id IN (SELECT root_id FROM #{self.class.table_name} WHERE id IN (?))", sets_to_lock).lock.ids
155
        else
156
          lock = true
155 157
          # This can be problematic if root_id or parent root_id changes
156 158
          # before locking
157 159
          sets_to_lock = [root_id, parent.try(:root_id)].compact.uniq
158
          self.class.reorder(:id).where(:root_id => sets_to_lock).lock(lock).ids
159
        else
160
          sets_to_lock = [id, parent_id].compact
161
          self.class.reorder(:id).where("root_id IN (SELECT root_id FROM #{self.class.table_name} WHERE id IN (?))", sets_to_lock).lock.ids
160
          if self.class.connection.adapter_name =~ /sqlserver/i
161
            # Custom lock for SQLServer
162
            lock = "WITH (ROWLOCK HOLDLOCK UPDLOCK)"
163
          end
164
          # No need to instanciate records
165
          self.class.connection.execute(self.class.reorder(:id).where(:root_id => sets_to_lock).lock.to_sql)
162 166
        end
163 167
      end
164 168

  
(1-1/2)