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事务

 

 

 

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

默认行为

mongodb事务

 

 

 


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

使用majority

mongodb事务

 

 


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

j:true

mongodb事务

 

 


写到journal文件才返回

mongodb事务

 

注意事项

mongodb事务

 

 


读操作

mongodb事务

 

 

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事务

 

 


readPreference场景举例

mongodb事务

 

 


readPreference与Tag

mongodb事务

 

 


readPreference配置

mongodb事务

 

 


实验

mongodb事务

 

 


注意事项

mongodb事务

 

 


什么是readConcern

mongodb事务

 

 

 

local和available

mongodb事务

 

 


注意事项

mongodb事务

 

 


readConcern:majority

mongodb事务

 

 


majority实现方式

mongodb事务

 

 


实验 

 mongodb事务

 

 

mongodb事务

 

 mongodb事务

 

 


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

mongodb事务

 

 


readConcern:linearizable

mongodb事务

 

 


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

snapshot

mongodb事务

 

 

多文档事务

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

mongodb事务

 

 mongodb事务

 

 

使用方法

mongodb事务

 

 


事务的隔离级别

mongodb事务

 

 


实验

mongodb事务

 

 


可重复读

mongodb事务

 

 


事务写机制

 mongodb事务

 

 mongodb事务

 

 


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

 mongodb事务

 

 

注意事项

mongodb事务

 

 



mongodb事务

上一篇:jQuery UI Datepicker使用介绍


下一篇:MySQL中关于事务的总结