Exemplo n.º 1
0
        /// <summary>
        /// Always returns a new instance of <see cref="PooledSession"/>. The new instance can:
        /// 1. represent the same session as this one, but will have a fresh transaction of the
        /// same type as this <see cref="PooledSession"/> did.
        /// 2. represent an entirely different session with a fresh transaction of the same type
        /// as this <see cref="PooledSession"/> did.
        /// This method will always try to get a fresh transaction for this session.
        /// If the session has expired or it fails to get a fresh transaction, then it will
        /// acquire a session in the normal way.
        /// This <see cref="PooledSession"/> instance will be disposed of to ensure that all operations
        /// with the underlying session are done through the new instance.
        /// </summary>
        /// <remarks>
        /// Use this method when executing operations that are best done using the same
        /// session. For instance, when retrying aborted commits it is better if the transaction work and commit
        /// are retried with the same session, because after each abort the sessions' lock priority increments.
        /// </remarks>
        /// <returns>A new instance of <see cref="PooledSession"/>.</returns>
        /// <exception cref="InvalidOperationException">If this <see cref="PooledSession.TransactionMode"/>
        /// is <see cref="ModeOneofCase.None"/>.</exception>
        public Task <PooledSession> WithFreshTransactionOrNewAsync(TransactionOptions transactionOptions, CancellationToken cancellationToken)
        {
            CheckNotDisposed();
            GaxPreconditions.CheckNotNull(transactionOptions, nameof(transactionOptions));
            GaxPreconditions.CheckArgument(transactionOptions.ModeCase == TransactionMode, nameof(transactionOptions), $"{nameof(TransactionOptions)} should be of the same type as this session's {nameof(TransactionMode)} which is {TransactionMode}");

            // Calling AfterReset() will mark this instance as disposed.
            // The pool will take care of releasing back to the pool if needed.
            return(_pool.WithFreshTransactionOrNewAsync(AfterReset(), transactionOptions, cancellationToken));
        }