2014-09-06 57 views
0

我試圖在django中實現這個原子事務,並且我正在關注文檔中的this example。但是,儘管回滾,但變化仍然存在。我搜索了類似的問題,最常見的原因似乎是在原子塊內捕獲完整性錯誤,但我沒有這樣做。 以下是我的Django代碼:在原子事務中插入的數據在回滾後仍然存在

def get_lead_alert_data(params): 
    with transaction.atomic(): 
     with acquire_lead_lock(): 
      caller = params['caller'] 
      via = params['via'] 
      called = params['called'] 
      leadphone = LeadsPhone.objects.filter(phone_number=caller, brokerage__isnull=True).first() 
      if leadphone: 
       lead_id = leadphone.lead_id 
      else: 
       lead_id = create_lead_from_inbound_call(caller, called) 
     created, requirement = get_or_create_requirement_from_inbound_call(via, lead_id) 
     picking_agent = Users.objects.get(phone_mobile=called) 
     if created: 
      RequirementAssignment.objects.create(requirement=requirement, agent=picking_agent) 
      assigned_to = picking_agent.user_name 
     else: 
      assigned_requirement = RequirementAssignment.objects.filter(brokerage__active=True, 
                     requirement=requirement).first() #There will be only one such requirement 
      if not assigned_requirement.agent: 
       assigned_requirement.agent = picking_agent 
       assigned_requirement.save() 
      assigned_to = assigned_requirement.agent.user_name if assigned_requirement else 'nobody' 
    return {'lead_id': lead_id, 'assigned_to': assigned_to, 'picking_by': picking_agent.user_name} 

我也查得到MySQL的日誌,它確實調用回滾,但更改存留。

3043 Connect [email protected] on reserve_db_2 
       3043 Query  SET NAMES utf8 
       3043 Query  set autocommit=0 
       3043 Query  set autocommit=1 
       3043 Query  SET SQL_AUTO_IS_NULL = 0 
       3043 Query  set autocommit=0 
       3043 Query  lock table person write, leads write, leads_phones write, leads_emails write, requirements write, tele_phones read 
       3043 Query  SELECT `leads_phones`.`id`, `leads_phones`.`lead_id`, `leads_phones`.`phone_number`, `leads_phones`.`brokerage_id`, `leads_phones`.`created` FROM `leads_phones` WHERE (`leads_phones`.`phone_number` = '9899696089' AND `leads_phones`.`brokerage_id` IS NULL) ORDER BY `leads_phones`.`id` ASC LIMIT 1 
       3043 Query  INSERT INTO `person` (`user_id`, `fullname`, `mobile_no`, `fb_location`, `fb_email`, `fb_aboutme`, `fb_avatar`, `goog_email`, `goog_avatar`, `uploaded_avatar`, `first_name`, `last_name`, `description`, `address`, `is_admin`, `reviewer_badge`, `title`, `phone_home`, `phone_work`, `phone_other`, `phone_fax`, `status`, `address_street`, `address_city`, `address_region_id`, `address_country`, `address_postalcode`, `created`, `last_updated`, `created_by`, `modified_by`, `deleted`) VALUES (NULL, '', NULL, '', '', '', '', '', '', '', '', '', '', '', NULL, '1ST TIME REVIEWER', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2014-09-06 11:12:03', '2014-09-06 11:12:03', '', NULL, 0) 
140906 16:40:05 3043 Query  INSERT INTO `leads` (`id`, `date_entered`, `date_modified`, `modified_user_id`, `created_by`, `description`, `deleted`, `assigned_user_id`, `salutation`, `first_name`, `middle_name`, `last_name`, `title`, `department`, `do_not_call`, `primary_email_address`, `secondary_email_address`, `phone_home`, `phone_mobile`, `phone_work`, `phone_other`, `phone_fax`, `primary_address_street`, `primary_address_city`, `primary_address_state`, `primary_address_postalcode`, `primary_address_country`, `alt_address_street`, `alt_address_city`, `alt_address_state`, `alt_address_postalcode`, `alt_address_country`, `converted`, `refered_by`, `lead_source_description`, `status`, `status_description`, `reports_to_id`, `residence_phone`, `citizenship`, `primary_address_street_by_agent`, `office_location`, `owned_rented`, `owned_rented_by_agent`, `unique_id`, `reason_for_status_change`, `annual_income`, `annual_income_by_agent`, `designation`, `executive_level`, `executive_level_by_agent`, `present_company`, `website`, `lead_type_fav`, `lead_type_c`, `facebook_url`, `linkedin_url`, `twitter_url`, `google_plus_url`, `assigned_user_date`, `worked_by_tele`, `worked_by_sales`, `off_campaign_id`, `activity_done`, `activity_completed`, `queue_name`, `queue_description`, `history_notes`, `lead_category`, `trans_type`, `potential`, `referral_remark`, `referral_name`, `referral_no`, `referral_email`, `primary_secondary_lead`, `met_face_to_face`, `met_site_visit`, `met_final_negotiation`, `total_met`, `is_duplicate`, `is_duplicate_date`, `queue_abort_remark`, `referer_url`, `landing_url`, `leadpage_url`, `lead_projects`, `lead_projects_ids`, `lead_max_budget`, `lead_source`, `person_id`, `brokerage_id`, `lead_parent_id`) VALUES ('1440bb40-4f8a-4f87-917f-6aca0c758711', NULL, NULL, NULL, '', NULL, 0, '', NULL, NULL, NULL, '', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '', NULL, NULL, '', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 315601, NULL, NULL) 
       3043 Query  INSERT INTO `leads_phones` (`lead_id`, `phone_number`, `brokerage_id`, `created`) VALUES ('1440bb40-4f8a-4f87-917f-6aca0c758711', '9899696089', NULL, '2014-09-06 11:12:03') 
       3043 Query  SELECT `tele_phones`.`id`, `tele_phones`.`source_id`, `tele_phones`.`project_id`, `tele_phones`.`locality_id`, `tele_phones`.`cluster_id`, `tele_phones`.`city_id` FROM `tele_phones` WHERE `tele_phones`.`id` = '3314892' LIMIT 21 
       3043 Query  INSERT INTO `requirements` (`id`, `req_unique_id`, `lead_id`, `user_id`, `name`, `date_entered`, `date_modified`, `created_by`, `modified_user_id`, `assigned_user_id`, `deleted`, `req_type`, `category`, `bhk`, `unit_type`, `construction_phase`, `main_entrance_facing`, `balcony_facing`, `furnish_state`, `plc`, `locality`, `cluster`, `city`, `region`, `project`, `plot_area`, `super_area`, `price_sft_syd`, `price`, `total_price`, `cash_in_hand`, `need_loan`, `description`, `is_active_req`) VALUES ('63494d0d-88f8-44f5-816c-af4bb5ec439e', NULL, '1440bb40-4f8a-4f87-917f-6aca0c758711', NULL, '', NULL, NULL, '', '', '', NULL, '', '', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, 1, 1, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, 1) 
       3043 Query  unlock tables 
       3043 Query  rollback 
       3043 Query  set autocommit=1 
+0

你沒有使用InnoDB表嗎? – MrTux 2014-09-06 12:03:10

+0

是的,他們都是innoDB – lmc 2014-09-06 12:14:25

回答

1

Ohk,我檢查了mysql文檔。看起來像解鎖表implicitly causes a commit如果有任何鎖定表,這是在我的情況。必須找到解決方法。

相關問題