private async Task <PooledSession> BeginTransactionAsync(PooledSession session, TransactionOptions options, CancellationToken cancellationToken) { // While we're creating a transaction, it's as if we're preparing a new session - it's a period of time // where there's already an RPC in flight, and when it completes a session will be available. Interlocked.Increment(ref _inFlightSessionCreationCount); var request = new BeginTransactionRequest { Options = options }; try { var transaction = await session.BeginTransactionAsync(request, Options.Timeout, cancellationToken).ConfigureAwait(false); return(session.WithTransaction(transaction.Id, options.ModeCase)); } finally { Interlocked.Decrement(ref _inFlightSessionCreationCount); } }
/// <summary> /// Refreshes a session by setting executing a trivial SELECT SQL statement. /// This is performed via the client session itself so it can update its next refresh time. /// </summary> private async Task RefreshAsync(PooledSession session) { // While we're refreshing a session, it's as if we're creating a new one - it's a period of time // where there's already an RPC in flight, and when it completes a session will be available. Interlocked.Increment(ref _inFlightSessionCreationCount); try { await session.ExecuteSqlAsync(new ExecuteSqlRequest { Sql = "SELECT 1" }, Options.Timeout, CancellationToken.None).ConfigureAwait(false); } catch (RpcException e) { Parent._logger.Warn("Failed to refresh session. Session will be deleted.", e); EvictSession(session); return; } finally { Interlocked.Decrement(ref _inFlightSessionCreationCount); } // We now definitely don't have a transaction. ReleaseInactiveSession(session.WithTransaction(null, ModeOneofCase.None), maybeCreateReadWriteTransaction: true); }