0
点赞
收藏
分享

微信扫一扫

mongodb事务

mongo开启事物支持如下配置:

/**
* Configuration options for a transaction.
* @see https://mongodb.github.io/node-mongodb-native/3.6/api/global.html#TransactionOptions
*/
export interface TransactionOptions {
readConcern?: ReadConcern;
writeConcern?: WriteConcern;
readPreference?: ReadPreferenceOrMode;
}

 

写操作

/**
* A MongoDB WriteConcern, which describes the level of acknowledgement
* requested from MongoDB for write operations.
* @see https://mongodb.github.io/node-mongodb-native/3.6/api/global.html#WriteConcern
*/
interface WriteConcern {
/**
* requests acknowledgement that the write operation has
* propagated to a specified number of mongod hosts
* @default 1
*/
w?: number | 'majority' | string;
/**
* requests acknowledgement from MongoDB that the write operation has
* been written to the journal
* @default false
*/
j?: boolean;
/**
* a time limit, in milliseconds, for the write concern
*/
wtimeout?: number;
}

 

什么是writeConcern?

mongodb事务_html

 

 

 

majority:超过半数以上的节点才算成功,常用于经常被扩容的节点

默认行为

mongodb事务_mongodb_02

 

 

 

写入x=1立即返回,并没有等到从节点复制完成

使用majority

mongodb事务_mongodb_03

 

 

写入x=1,等到从节点1完成复制,再返回, 因为3个节点,所以2个节点成功就可以返回了

j:true

mongodb事务_mongodb_04

 

 

写到journal文件才返回

mongodb事务_html_05

 

注意事项

mongodb事务_github_06

 

 

读操作

mongodb事务_mongodb_07

 

 

export type ReadPreferenceTags = ReadonlyArray<Record<string, string>>;
export type ReadPreferenceMode = 'primary' | 'primaryPreferred' | 'secondary' | 'secondaryPreferred' | 'nearest';
export type ReadPreferenceOrMode = ReadPreference | ReadPreferenceMode;
export type ReadPreferenceOptions = {
/** Server mode in which the same query is dispatched in parallel to multiple replica set members. */
hedge?: {
/** Explicitly enable or disable hedged reads. */
enabled?: boolean;
};
/**
* Max secondary read staleness in seconds, Minimum value is 90 seconds.
*/
maxStalenessSeconds?: number;
};

/**
* The **ReadPreference** class represents a MongoDB ReadPreference and is used to construct connections.
* @see https://docs.mongodb.com/manual/core/read-preference/
*/
export class ReadPreference {
constructor(mode: ReadPreferenceMode, tags: object, options?: ReadPreferenceOptions);
mode: ReadPreferenceMode;
tags: ReadPreferenceTags;
static PRIMARY: 'primary';
static PRIMARY_PREFERRED: 'primaryPreferred';
static SECONDARY: 'secondary';
static SECONDARY_PREFERRED: 'secondaryPreferred';
static NEAREST: 'nearest';
isValid(mode: ReadPreferenceMode | string): boolean;
static isValid(mode: string): boolean;
/**
* Indicates that this readPreference needs the "slaveOk" bit when sent over the wire
* @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
*/
slaveOk(): boolean;
/**
* Are the two read preference equal
* @param readPreference - the read preference with which to check equality
* @return `true` if the two {@link ReadPreference}s are equivalent
*/
equals(readPreference: ReadPreference): boolean;
}

 

/** @see https://mongodb.github.io/node-mongodb-native/3.6/api/global.html#ReadConcern */
type ReadConcernLevel = 'local' | 'available' | 'majority' | 'linearizable' | 'snapshot';

/**
* The MongoDB ReadConcern, which allows for control of the consistency and isolation properties
* of the data read from replica sets and replica set shards.
* @see https://mongodb.github.io/node-mongodb-native/3.6/api/global.html#ReadConcern
*/
export interface ReadConcern {
level: ReadConcernLevel;
}

 

什么是readPreference

mongodb事务_mongodb_08

 

 

readPreference场景举例

mongodb事务_github_09

 

 

readPreference与Tag

mongodb事务_html_10

 

 

readPreference配置

mongodb事务_mongodb_11

 

 

实验

mongodb事务_mongodb_12

 

 

注意事项

mongodb事务_html_13

 

 

什么是readConcern

mongodb事务_html_14

 

 

 

local和available

mongodb事务_github_15

 

 

注意事项

mongodb事务_mongodb_16

 

 

readConcern:majority

mongodb事务_github_17

 

 

majority实现方式

mongodb事务_mongodb_18

 

 

实验 

 

mongodb事务_github_19

 

 

 

 

mongodb事务_html_20

 

 

readConcern:如何实现安全的读写分离

mongodb事务_github_21

 

 

readConcern:linearizable

mongodb事务_html_22

 

 

选举过程中,同时old节点还能工作

snapshot

mongodb事务_mongodb_23

 

 

多文档事务

4.0只支持复制集,不支持分片

mongodb事务_html_24

 

 

mongodb事务_html_25

 

 

使用方法

mongodb事务_github_26

 

 

事务的隔离级别

mongodb事务_mongodb_27

 

 

实验

mongodb事务_github_28

 

 

可重复读

mongodb事务_html_29

 

 

事务写机制

 

mongodb事务_mongodb_30

 

 

mongodb事务_github_31

 

 

等另一个提交后,重启事务就好了 session.abortTransaction() 

 

mongodb事务_html_32

 

 

注意事项

mongodb事务_html_33

 

 




举报

相关推荐

0 条评论