after_create and after_commit
A relational database, like mysql, provides transactions to wrap several operations in one unit, make them all pass or all fail. All isolation levels except READ UNCOMMITTED don't allow read data changes until they are committed in other transaction. If you don't realize it, you probably introduce some unexpected errors.
Before
It's common to generate a background job to send emails, tweets or post to facebook wall, like
class Notification < ActiveRecord::Base
after_create :asyns_send_notification
def async_send_notification
NotificationWorker.async_send_notification({:notification_id => id})
end
end
class NotificationWorker < Workling::Base
def send_notification(params)
notification = Notification.find(params[:notification_id])
user = notification.user
# send notification to user's friends by email
end
end
It looks fine, every time it creates a notification, generates an asynchronous worker, assigns notification_id to the worker, in the worker it finds the notification by id, then sends notification by email.
You won't see any issue in development, as local db can commit fast. But in production server, db traffic might be huge, worker probably finish faster than transaction commit. e.g.
main process | worker process |
---|---|
BEGIN | |
INSERT INTO notifications(message, user_id) values('notification message', 1) | |
# return id 10 for newly-created notification | |
SELECT * FROM notifications WHERE id = 10 | |
COMMIT |
In this case, the worker process query the newly-created notification before main process commits the transaction, it will raise NotFoundError, because transaction in worker process can't read uncommitted notification from transaction in main process.
Refactor
So we should tell activerecord to generate notification worker after notification insertion transaction committed.
class Notification < ActiveRecord::Base
after_commit :asyns_send_notification, :on => :create
def async_send_notification
NotificationWorker.async_send_notification({:notification_id => id})
end
end
Now the transactions order becomes
main process | worker process |
---|---|
BEGIN | |
INSERT INTO notifications(message, user_id) values('notification message', 1) | |
# return id 10 for newly-created notification | |
COMMIT | |
SELECT * FROM notifications WHERE id = 10 |
Worker process won't receive NotFoundErrors any more.
For those callbacks that no need to execute in one transaction, you should always use after_commit to avoid unexpected errors.
after_commit is introduced from rails 3, if you use rails 2, please check out after_commit gem instead.
after_create and after_commit的更多相关文章
- 开发新手最容易犯的50个 Ruby on Rails 错误(1)
[编者按]本文最早发布与 JETRuby 博客,主要介绍了开发新手最容易犯的 Ruby 错误.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 一年前,我们创立了以 "Rubyboo ...
- Bullet:MySQL增强半同步参数rpl_semi_sync_master_wait_point值AFTER_SYNC和AFTER_COMMIT的对比实验
MySQL 5.7.22启用增强半同步复制 MySQL对该参数值的描述 Semisync can wait for slave ACKs at one of two points, AFTER_SYN ...
- 《Ruby on Rails教程》学习笔记
本文是我在阅读 Ruby on Rails 教程的简体中文版时所做的摘录,以及学习时寻找的补充知识.补充知识主要来自于 Ruby on Rails 實戰聖經. Asset Pipeline 在最新版 ...
- Rails5 Model Document
创建: 2017/06/09 更新: 2017/06/21 更新: 2017/06/23 对待未完成的追加# TODO: 更新: 2017/06/29 修正文件名db/seed.rb ---> ...
- [MySQL Reference Manual] 18 复制
18 复制 18 复制 18.1 复制配置 18.1.1 基于Binary Log的数据库复制配置 18.1.2 配置基于Binary log的复制 18.1.2.1 设置复制master的配置 18 ...
- mysql半同步(semi-sync)源码实现
mysql复制简单介绍了mysql semi-sync的出现的原因,并说明了semi-sync如何保证不丢数据.这篇文章主要侧重于semi-sync的实现,结合源码将semi-sync的实现过程展现给 ...
- MySQL 5.7 Replication 相关新功能说明
背景: MySQL5.7在主从复制上面相对之前版本多了一些新特性,包括多源复制.基于组提交的并行复制.在线修改Replication Filter.GTID增强.半同步复制增强等.因为都是和复制相关, ...
- MySQL 半同步复制+MMM架构
200 ? "200px" : this.width)!important;} --> 介绍 上篇文章介绍了MMM架构的实现方法,但是上篇文章的MMM方案的复制是异步复制,异 ...
- MySQL半同步复制
从MySQL5.5开始,MySQL以插件的形式支持半同步复制.如何理解半同步呢?首先我们来看看异步,全同步的概念 异步复制(Asynchronous replication) MySQL默认的复制即是 ...
随机推荐
- 集体智慧编程-discovering groups
这一章讲的是利用聚集算法对blog进行分类. 首先是构造数据,找到一组blog,每个blog包含一组单词.这样就形成了(blog-name, word*)*的数据结构. 在构造该数据结构的过程中,还需 ...
- linux系统编程之信号(三):信号安装、signal、kill,arise讲解
一,信号安装 如果进程要处理某一信号,那么就要在进程中安装该信号.安装信号主要用来确定信号值及进程针对该信号值的动作之间的映射关系,即进程将要处理哪个信号:该信号被传递给进程时,将执行何种操作. li ...
- Android实现自带横线的EditText
(一)问题 怎样实现带有横栏的EditText(像记事本的编辑界面那样)? (二)初步思路 1.通过修改EditText背景来实现(系统背景是一个框形图片,内部透明,替换为一个带有横栏的图片即可) 2 ...
- Python: 读写Excel(openpyxl / win32com.client)
项目周报汇报的时候要做数据汇总,总是要从不同的excel文件中去获取数据最后汇总到一个excel表里面,所以决定用python直接写个自动化脚本来自动执行. 用python来读写excel,目前找了2 ...
- 深入理解Aspnet Core之Identity(1)
最近学习asp.netcore 打算写出来和大家分享,我计划先写Identity部分,会从开始asp.netocre identity的简单实用开始,然后再去讲解主要的类和自定义这些类. 主题:asp ...
- C#之使用CefSharp创建客户端
安装NuGet包 在Visio studio中右击解决方案,选择管理NuGet包,搜索安装CefSharp.WinForms. 配置工作 (1)首先右击项目选择属性,在"生成"选项 ...
- 命名空间“Microsoft”中不存在类型或命名空间名“Reporting”(是否缺少程序集引用?)
IDE升级到VS2017之后,出现了如题所示的报错,重新引用DLL的方法如下: 1.右键引用,选择添加引用. 2.左侧选择浏览,下面点击浏览按钮. 3.分别添加Microsoft.ReportView ...
- 什么是fortran语言之fortran语言入门
Fortran源自于“公式翻译”(英语:FormulaTranslation)的缩写,是一种编程语言.它是世界上最早出现的计算机高级程序设计语言,广泛应用于科学和工程计算领域.FORTRAN语言以其特 ...
- ovs 源mac, 目的src 互换
push:NXM_OF_ETH_SRC[],push:NXM_OF_ETH_DST[],pop:NXM_OF_ETH_SRC[],pop:NXM_OF_ETH_DST[] 1:把src mac推到栈顶 ...
- 201621123018《Java程序设计》第1周学习报告
1.<Java程序设计>第1周学习报告 1.本周学习报告 关键字:Java的发展.Java语言的特点.JDK.JRE.JVE.Java开发工具.Java环境配置.Java是一种面向对象的程 ...