0

xml ファイルの顧客からロードするメソッドがあります。ファイルをダウンロードする前に、xml ファイルに含まれていないすべての顧客は有効性 false に入れられます。次に、既存の顧客の読み込みと更新を開始します。メソッド全体をトランザクションでラップしました。しかし、ダウンロード クライアントを意図的に間違ったもの (検証に合格しない) にしようとすると、トランザクション全体がロールバックされません。私は何を間違っていますか?トランザクションレールで作業するには?

コード:

if customers_upload
  EXCHANGE_LOGGER.info("Start customers exchange")
  Customer.transaction do
    begin
      customers = xml.elements.to_a("//customer")

      customers_external_keys = []
      customers.each do |customer| 
        customers_external_keys << customer.elements['external_key'].text
      end          
      customers_false = Customer.where("external_key NOT IN (?)", customers_external_keys)
      customers_false.each do |customer_false|
        if customer_false.validity
          customer_false.update_attributes(validity: false)              
        end
      end
      EXCHANGE_LOGGER.info("#{customers_false.count} update validity in false")

      customers.each do |customer| 
        customer_name = customer.elements['name'].text
        customer_external_key = customer.elements['external_key'].text
        customer_address = customer.elements['address'].text
        customer_debt = customer.elements['debt'].text
        customer_db = Customer.find_by_external_key(customer_external_key)
        if !customer_db
          new_customer = Customer.create(name: customer_name, external_key: customer_external_key, address: customer_address, debt: customer_debt)
          EXCHANGE_LOGGER.info("#Create new customer #{customer_name}")
        else 
          if !customer_db.validity
            customer_db.update_attributes(name: customer_name, address: customer_address, debt: customer_debt, validity: true)
            EXCHANGE_LOGGER.info("#Change validity true and update customer #{customer_name}")            
          else
            customer_db.update_attributes(name: customer_name, address: customer_address, debt: customer_debt)
            EXCHANGE_LOGGER.info("#Update customer #{customer_name}")
          end        
        end
      end

    rescue => e
      if e.present?
        EXCHANGE_LOGGER.error("Customers not exchanged, message: #{e}")
        raise ActiveRecord::Rollback, "Call tech support!"
      end          
    end        
  end  
end

exchange.log の内容は次のとおりです。

2013-11-29 10:53:23 INFO Start customers exchange
2013-11-29 10:53:33 INFO 3981 update validity in false
2013-11-29 10:53:33 ERROR Customers not exchanged, message: undefined method `text 'for nil: NilClass

development.log の内容は次のとおりです。

 

 Customer Exists (0.2ms) SELECT 1 AS one FROM customersWHERE ( customers. External_key= 'CB001820' AND customers. Id! = 3979 ) LIMIT 1 (0.1ms) UPDATE customersSET validity= 0 , updated_at= '2013- 11 -29 10:53:33 'WHERE customers. Id= 3979 Customer Exists (0.2ms) SELECT 1 AS one FROM customersWHERE ( customers. External_key= 'CB001826' AND customers. Id! = 3980 ) LIMIT 1 (0.1ms) UPDATE customersSET validity= 0 , updated_at= '2013- 11 -29 10:53:33 'どこcustomersで。Id= 3980 Customer Exists (0.2ms) SELECT 1 AS one FROM customersWHERE ( customers. External_key= 'CB001822' AND customers. Id! = 3981 ) LIMIT 1 (0.1ms) UPDATE customersSET validity= 0 , updated_at= '2013- 11 -29 10:53:33 'WHERE customers. Id= 3981 (2.2ms) SELECT COUNT (*) FROM customersWHERE (external_key NOT IN ('12312')) (0.1ms) ROLLBACK

同様に ROLLBACK が最後に表示されますが、すべてのクライアントは引き続き有効です: (

4

1 に答える 1

0

トランザクションをサポートするデータベース内のテーブルを使用する必要があります。たとえば、InnoDB

于 2013-12-23T06:46:29.580 に答える