数据并发处理

ACID性质

  • 原子性(Atomicity): 要么全部被执行,要么都不执行;
  • 一致性(Consistency): 满足完整性约束;
  • 隔离性(Isolation): 不应影响其他事务的执行;
  • 持久性(Durability : 永久保存在数据库中;

隔离级别

  • 未提交读(Read uncommitted): 允许脏读,可以看到其他事务尚未提交的修改;
  • 提交读(Read committed): 写锁一直保持到事务结束,读锁在SELECT操作完成后马上释放,不要求范围锁;
  • 可重复读(Repeatable reads) : 读锁和写锁一直保持到事务结束,不要求范围锁;
  • 可序列化(Serializable): 读锁和写锁保持直到事务结束后才能释放,查询中使用“WHERE”子句来获得一个范围锁;
  • 较高的隔离级别能更好地保证数据一致性,但反过来会影响程序的并发性能;

读现象

  • 脏读: 当一个事务允许读取另外一个事务修改但未提交的数据;
  • 不可重复读: 一行数据获取两遍得到不同的结果(发生在SELECT 操作没有获得读锁或者SELECT执行完后马上释放了读锁)
  • 幻读: 两个完全相同的查询语句执行得到不同的结果集(没有获取范围锁的情况下执行SELECT ... WHERE操作可能会发生);

隔离级别vs读现象

隔离级别 脏读 不可重复读 幻读
未提交读 可能发生 可能发生 可能发生
提交读 可能发生 可能发生
可重复读 可能发生
可序列化

隔离级别vs 锁持续时间

  • s: 锁持续到当前语句执行完毕
  • c: 锁会持续到事务提交
隔离级别 写操作 读操作 范围操作
未提交读 s s s
提交读 c s s
可重复读 c c s
可序列化 c c c

数据库默认隔离级别

  • MySQL: Repeatable read;
  • PG : Read committed;

loopback事务分离处理

一个例子

let currnetTx = null;

return Vote.count({
userId,
title: 'voteA:最美coser',
created_at: {
between: [`2015-12-${now.getDate()}`, `2015-12-${now.getDate() + 1}`]
}
}).then(count=> {
if(count > 0) {
return res.send({code:2, msg: 'voted before'});
} return Vote.beginTransaction({
isolateionLevel: Vote.Transaction.REPEATABLE_READ,
tiemout: 30000
}).then(tx=> {
currentTx = tx
if(userId === staticId) return;
return Vote.create({userId, itemId: instanceId, title: 'voteA:最美coser'}, {transaction: currentTx});
}).then(()=>{
return VoteA.findOne({where: {itemId: instanceId}}, {transaction: currentTx})
}).then(voteA=> {
return voteA.updateAttributes({count: ++voteA.count}, {transaction: currentTx})
}).then(()=> {
if(currentTx) currentTx.commit();
console.log(`最美coser: userId-${userId} vote itemId-${instanceId}`);
return res.send({code: 1, msg: 'success!'});
})
}).catch(err=> {
if(currentTx) currentTx.rollback();
console.log(err);
return res.status(500).end();
})

loopback类型

  • loopback中获取的时间类型就为Date对象;
  • loopback中使用查询涉及到时间时使用UTC时间;

loopback一个插入数据脚本例子

//seedDate

export const figureCategories = {
pvc: {name: '静态PVC'},
GK: {name: 'GK'},
figma: {name: 'figma'},
pf: {name: 'PF'},
human: {name: '人形'}
} export const brandData = {
pvc: [
{name: 'Bandai/万代'},
{name: 'Goodsmile'},
{name: 'MegaHouse'}
],
GK: [
{name: 'Bandai/万代'},
{name: 'Goodsmile'},
{name: 'MegaHouse'}
],
figma: [
{name: 'Bandai/万代'},
{name: 'Goodsmile'},
{name: 'MegaHouse'}
],
pf: [
{name: 'Bandai/万代'},
{name: 'Goodsmile'},
{name: 'MegaHouse'}
],
human: [
{name: 'Bandai/万代'},
{name: 'Goodsmile'},
{name: 'MegaHouse'}
]
} //seed import Promise from 'bluebird';
import {figureCategories, brandData} from './seedData-01.js'; export default function(app, done) {
const FigureCategory = app.models.figureCategory;
const FigureBrand = app.models.figureBrand; function initSeedData(category, brands) {
return FigureCategory.findOrCreate({
where: {name: category.name}
}, category).then(category=>{
return Promise.resolve(brands).map(brand=>{
return FigureBrand.findOrCreate({
where: {name: brand.name}
}, brand).then(brand=>{
return category[0].brands.add(brand[0]).catch(console.log);
})
}, {concurrency: 1});
}).catch(console.log);
} Promise.resolve(Object.keys(figureCategories)).map(key=>{
return initSeedData(figureCategories[key], brandData[key])
}, {concurrency: 1}).then(()=>{
done();
}).catch(done);
}

数据库层面的匹配

  • scope设置在json文件
{
"scope": {
"limit": 10,
"where": {
"status": "online"
}
}
}

loopback 05的更多相关文章

  1. [React] 05 - Route: connect with ExpressJS

    基础: 初步理解:Node.js Express 框架 参见:[Node.js] 08 - Web Server and REST API 进阶: Ref: 如何系统地学习 Express?[该网页有 ...

  2. java.io.IOException: Unable to establish loopback connection

    1.错误描述 Starting preview server on port 8080 Modules: HTML5 (/HTML5) 2017-06-17 11:13:04.823:INFO::ma ...

  3. Java学习笔记(05)

    目录: static的用法 主函数的定义 增强for的循环 单例设计模式 封装 一.Static的用法 1.对象的内存分析 对象的引用变量是存在于栈区,而在堆区开辟了一块内存空间,调用对象给成员变量赋 ...

  4. iOS系列 基础篇 05 视图鼻祖 - UIView

    iOS系列 基础篇 05 视图鼻祖 - UIView 目录: UIView“家族” 应用界面的构建层次 视图分类 最后 在Cocoa和Cocoa Touch框架中,“根”类时NSObject类.同样, ...

  5. 【web开发 | 移动APP开发】 Web 移动开发指南(2017.01.05更新)

    版本记录 - 版本1.0 创建文章(2016.12.30) - 版本1.1 更正了hybird相关知识:增加了参考文章(2017.01.05): + Web APP更正为响应式移动站点与页面,简称响应 ...

  6. javaSE基础05

    javaSE基础05:面向对象 一.数组 数组的内存管理 : 一块连续的空间来存储元素. Int [ ] arr = new int[ ]; 创建一个int类型的数组,arr只是一个变量,只是数组的一 ...

  7. Android 学习笔记之一 “Unable to establish loopback connection”

    今天碰到一个错误:Unable to establish loopback connection,在网上找各种方法都解决不了,后来看一个帖子说是要关闭系统防火墙,尝试了下还是不行.最后是进任务管理器杀 ...

  8. 异步编程系列第05章 Await究竟做了什么?

    p { display: block; margin: 3px 0 0 0; } --> 写在前面 在学异步,有位园友推荐了<async in C#5.0>,没找到中文版,恰巧也想提 ...

  9. loopback文档翻译

    最近在学习loopback,期间在strongloop的官网翻译了部分文章. 见:https://docs.strongloop.com/pages/viewpage.action?pageId=60 ...

随机推荐

  1. HTML超链接

    打开网页在 想要查看的位置右键单击   审查元素  则可以查看代码    点击图片右键单独打开  则可以查看图片位置 一.超链接 a标签   <a href="地址"> ...

  2. 【leetcode】 Generate Parentheses (middle)☆

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  3. 【leetcode】Search in Rotated Sorted Array II(middle)☆

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  4. UIView CALayer 的区别

    UIView与CALayer的区别,很详细 研究Core Animation已经有段时间了,关于Core Animation,网上没什么好的介绍.苹果网站上有篇专门的总结性介绍,但是似乎原理性的东西不 ...

  5. xmpp的bug

    [微分享]:事前必三思,事中要坚韧,事后莫悔恨,只有眼光看远些,脚步坚实些,人生方多些圆满,少些遗憾. xmpp的bug

  6. iOS 十六进制和字符串转换

    NSString *dictString = [dict JSONFragment];//组合成的. dictString==={"content":"Sadgfdfg& ...

  7. iOS 简单提示view

    +(void)showMessage:(NSString *)message{    UIWindow * window = [UIApplication sharedApplication].key ...

  8. JavaWeb学习之tomcat安装与运行、tomcat的目录结构、配置tomcat的管理用户、web项目目录、虚拟目录、虚拟主机(1)

    1.tomcat安装与运行双击tomcat目录下的bin/startup.bat,启动之后,输入http://localhost:8080,出现安装成功的提示,表示安装tomcat成功 2.tomca ...

  9. HTML5学习之拖放(十)

    l元素可以用于拖拽必须设置draggable="true"属性,img和a标签除外,她们两个默认就可以被拖拽 想做拖拽处理,就需要在Dom元素上监听拖放的事件:dragstart, ...

  10. sdut 2441 屠夫与狼

    屠夫和狼 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...