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 파일은 다음과 같습니다
고객 존재 (0.2ms)
customers
WHERE (customers
.External_key
= 'CB001820'및customers
Id
! = 3979) 제한 1 (0.1ms) 업데이트customers
SETvalidity
= 0,updated_at
= '2013- 11 -29 10:53:33'WHEREcustomers
.Id
는 = 3979 고객이 존재 (이 0.2ms)는customers
FROM 하나 하나를 선택합니다 (customers
.External_key
= 'CB001826'ANDcustomers
을.Id
! = 3980) LIMIT 1 (는 0.1ms)customers
SET를 UPDATEvalidity
= 0,updated_at
= '2013- 11 -29 10:53:33'WHEREcustomers
.Id
는 = 3980 고객이 존재 (이 0.2ms)는customers
FROM 하나 하나를 선택합니다 (customers
.External_key
= 'CB001822'ANDcustomers
을.Id
! = 3981) LIMIT 1 (는 0.1ms)customers
SET를 UPDATEvalidity
= 0,updated_at
= '2013- 11 -29 10:53:33'WHEREcustomers
. (external_key하지 IN ('12312')()는 0.1ms)가 ROLLBACK처럼
을 롤백 customers
FROM Id
= 3981 (2.2ms) SELECT COUNT (*)는 마지막에 나타나지만, 모든 클라이언트가 여전히 것 유효 : (
) tablin Inno_DB의 유형을 잊어 버렸습니다. :) – galievruslan