即使在回滚后,原子事务中插入的数据仍然存在

时间:2014-09-06 12:01:40

标签: mysql django transactions

我正在尝试在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   root@localhost 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

1 个答案:

答案 0 :(得分:1)

哦,我检查了mysql文档。看起来像解锁表implicitly causes a commit,如果有任何锁定表,在我的情况下。必须找到一个解决方法。