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?

 

 

 

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

默认行为

 

 

 


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

使用majority

 

 


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

j:true

 

 


写到journal文件才返回

 

注意事项

 

 


读操作

 

 

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

 

 


readPreference场景举例

 

 


readPreference与Tag

 

 


readPreference配置

 

 


实验

 

 


注意事项

 

 


什么是readConcern

 

 

 

local和available

 

 


注意事项

 

 


readConcern:majority

 

 


majority实现方式

 

 


实验 

 

 

 

 

 

 

 


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

 

 


readConcern:linearizable

 

 


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

snapshot

 

 

多文档事务

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

 

 

 

 

使用方法

 

 


事务的隔离级别

 

 


实验

 

 


可重复读

 

 


事务写机制

 

 

 

 

 


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

 

 

 

注意事项

 

 



posted @ 2021-05-25 18:41  苍青浪  阅读(602)  评论(0编辑  收藏  举报