在数据库事务期间,我想做一些其他事情-例如,查询ElasticSearch,然后将结果写入其他db表,然后再提交事务,这在逻辑上是我希望整个过程在逻辑上有意义。相同的交易,以太都成功或失败,但是我不确定这是否可行和好的做法。如果不是,那么在这种情况下是否有良好做法的建议?谢谢
我认为这是一个普遍的问题,如果有帮助,我正在使用postgresql。
答案 0 :(得分:0)
您可以使用INSERT INTO dw_coder_fact
(age,
gender,
country_citizen,
country_live,
city_population,
is_ethnic_minority,
language_at_home,
degree_type_id,
school_major,
marital_status_id,
has_children,
children_number,
employment_status,
employment_field_id,
is_software_dev,
months_programming,
is_underemployed,
income,
survey_date,
date_id,
attended_bootcamp,
bootcamp_fulljob_after,
bootcamp_postsalary,
hours_learning,
bootcamploanyesno,
money_for_learning,
has_student_debt,
student_debt_owe,
has_debt,
has_home_mortgage,
home_mortgage_owe,
financially_supporting,
has_financially_dependents,
job_apply_when,
type_of_company_pref,
job_role_interest,
job_where_pref,
expected_earning)
SELECT s.age,
s.gender,
s.countrycitizen,
s.countrylive,
s.citypopulation,
s.isethnicminority,
s.languageathome,
CASE s.schooldegree
WHEN "associate's degree" THEN 1
WHEN "bachelor's degree" THEN 2
WHEN 'high school diploma or equivalent (GED)' THEN 3
WHEN "master's degree (non-professional)" THEN 4
WHEN 'no high school (secondary school)' THEN 5
WHEN 'Ph.D.' THEN 6
WHEN 'professional degree (MBA, MD, JD, etc.)' THEN 7
WHEN 'some college credit, no degree' THEN 8
WHEN 'some high school' THEN 9
WHEN 'trade, technical. or vocational training' THEN 10
ELSE 11
end AS DegreeTypeDescription,
s.schoolmajor,
CASE s.maritalstatus
WHEN 'divorced' THEN 1
WHEN 'married or domestic partnership' THEN 2
WHEN 'separated' THEN 3
WHEN 'single, never married' THEN 4
WHEN 'widowed' THEN 5
ELSE 6
end AS MaritalStatusDescription,
s.haschildren,
s.childrennumber,
s.employmentstatus,
CASE s.employmentfield
WHEN 'architecture or physical engineering' THEN 1
WHEN 'arts, entertainmant, sports, or media' THEN 2
WHEN 'constraction and extraction' THEN 3
WHEN 'education' THEN 4
WHEN 'farming, fishing, and forestry' THEN 5
WHEN 'finance' THEN 6
WHEN 'food and beverage' THEN 7
WHEN 'health care' THEN 8
WHEN 'law enforcement and fire and rescue' THEN 9
WHEN 'legal' THEN 10
WHEN 'office and administrative support' THEN 11
WHEN 'sales' THEN 12
WHEN 'software development' THEN 13
WHEN 'software development and IT' THEN 14
WHEN 'transportation' THEN 15
ELSE 16
end AS EmploymentFieldDescription,
s.issoftwaredev,
s.monthsprogramming,
s.isunderemployed,
s.income,
s.part1starttime,
t.date_id,
s.attendedbootcamp,
s.bootcampfulljobafter,
s.bootcamppostsalary,
s.hourslearning,
s.bootcamploanyesno,
s.moneyforlearning,
s.hasstudentdebt,
s.studentdebtowe,
s.hasdebt,
s.hashomemortgage,
s.homemortgageowe,
s.financiallysupporting,
s.hasfinancialdependents,
s.jobapplywhen,
s.jobpref,
s.jobroleinterest,
s.jobwherepref,
s.expectedearning
FROM new_coders_survey_data s,
dw_time_dim t;
和BEGIN
来控制交易。
这里是文档:
https://www.postgresql.org/docs/8.3/tutorial-transactions.html
答案 1 :(得分:0)
这是一个很好的问题,不幸的是没有直接的答案。一切都取决于。让我给你一些例子,说明我建议的方法:
a\ save data in DB
b\ fetch some extra data (rollback in case there's fail here)
c\ save extra data in DB
从某种意义上讲,整个操作似乎是原子事务。但是考虑c中失败的原因。如果此数据不可用-确定。但是,如果失败是暂时的网络问题?根据您的业务,您可能希望在交易后移动b /,甚至可能执行异步作业并稍后填充数据?
a\ save some data
b\ save data in an external system
c\ do some more stuff, commit transaction
这比较棘手。如果b \成功,但是b \与c之间出现故障怎么办?您如何回滚b中所做的更改?
可能还有更多,我只是想展示一下您对此的看法。
我的直觉告诉我交易内部发生的事件越少,就越容易推断正在发生的事情。如果您发现自己正在调试这种怪异的场景-也许对重新设计应用程序进行建模是合理的投资,这样您就可以在交易之外进行这些额外的调用。