* Create a client session. * @internal * @param client - The current client * @param sessionPool - The server session pool (Internal Class) * @param options - Optional settings * @param clientOptions - Optional settings provided when creating a MongoClient
(
client: MongoClient,
sessionPool: ServerSessionPool,
options: ClientSessionOptions,
clientOptions: MongoOptions
)
| 155 | * @param clientOptions - Optional settings provided when creating a MongoClient |
| 156 | */ |
| 157 | constructor( |
| 158 | client: MongoClient, |
| 159 | sessionPool: ServerSessionPool, |
| 160 | options: ClientSessionOptions, |
| 161 | clientOptions: MongoOptions |
| 162 | ) { |
| 163 | super(); |
| 164 | this.on('error', noop); |
| 165 | |
| 166 | if (client == null) { |
| 167 | // TODO(NODE-3483) |
| 168 | throw new MongoRuntimeError('ClientSession requires a MongoClient'); |
| 169 | } |
| 170 | |
| 171 | if (sessionPool == null || !(sessionPool instanceof ServerSessionPool)) { |
| 172 | // TODO(NODE-3483) |
| 173 | throw new MongoRuntimeError('ClientSession requires a ServerSessionPool'); |
| 174 | } |
| 175 | |
| 176 | options = options ?? {}; |
| 177 | |
| 178 | this.snapshotEnabled = options.snapshot === true; |
| 179 | if (options.causalConsistency === true && this.snapshotEnabled) { |
| 180 | throw new MongoInvalidArgumentError( |
| 181 | 'Properties "causalConsistency" and "snapshot" are mutually exclusive' |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | this.client = client; |
| 186 | this.sessionPool = sessionPool; |
| 187 | this.hasEnded = false; |
| 188 | this.clientOptions = clientOptions; |
| 189 | this.timeoutMS = options.defaultTimeoutMS ?? client.s.options?.timeoutMS; |
| 190 | |
| 191 | this.explicit = !!options.explicit; |
| 192 | this._serverSession = this.explicit ? this.sessionPool.acquire() : null; |
| 193 | this.txnNumberIncrement = 0; |
| 194 | |
| 195 | const defaultCausalConsistencyValue = this.explicit && options.snapshot !== true; |
| 196 | this.supports = { |
| 197 | // if we can enable causal consistency, do so by default |
| 198 | causalConsistency: options.causalConsistency ?? defaultCausalConsistencyValue |
| 199 | }; |
| 200 | |
| 201 | this.clusterTime = options.initialClusterTime; |
| 202 | |
| 203 | this.operationTime = undefined; |
| 204 | this.owner = options.owner; |
| 205 | this.defaultTransactionOptions = { ...options.defaultTransactionOptions }; |
| 206 | this.transaction = new Transaction(); |
| 207 | } |
| 208 | |
| 209 | /** The server id associated with this session */ |
| 210 | get id(): ServerSessionId | undefined { |