Esempio n. 1
0
    public async Task <FbTransaction> BeginTransactionAsync(FbTransactionOptions options, string transactionName, CancellationToken cancellationToken = default)
    {
        EnsureActiveTransaction();

        try
        {
            _activeTransaction = new FbTransaction(_owningConnection, IsolationLevel.Unspecified);
            await _activeTransaction.BeginTransactionAsync(options, cancellationToken).ConfigureAwait(false);

            if (transactionName != null)
            {
                _activeTransaction.Save(transactionName);
            }
        }
        catch (IscException ex)
        {
            await DisposeTransactionAsync(cancellationToken).ConfigureAwait(false);

            throw FbException.Create(ex);
        }

        return(_activeTransaction);
    }
    private async Task PrepareAsync(bool returnsSet, CancellationToken cancellationToken = default)
    {
        var innerConn = _connection.InnerConnection;

        // Check if	we have	a valid	transaction
        if (_transaction == null)
        {
            if (innerConn.IsEnlisted)
            {
                _transaction = innerConn.ActiveTransaction;
            }
            else
            {
                _implicitTransaction = true;
                _transaction         = new FbTransaction(_connection, _connection.ConnectionOptions.IsolationLevel);
                await _transaction.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);

                // Update Statement	transaction
                if (_statement != null)
                {
                    _statement.Transaction = _transaction.Transaction;
                }
            }
        }

        // Check if	we have	a valid	statement handle
        if (_statement == null)
        {
            _statement = innerConn.Database.CreateStatement(_transaction.Transaction);
        }

        // Prepare the statement if	needed
        if (!_statement.IsPrepared)
        {
            // Close the inner DataReader if needed
            //await DisposeReaderAsync(cancellationToken).ConfigureAwait(false);

            // Reformat the SQL statement if needed
            var sql = _commandText;

            try
            {
                (sql, _namedParameters) = NamedParametersParser.Parse(sql);
                // Try to prepare the command
                await _statement.PrepareAsync(sql, cancellationToken).ConfigureAwait(false);
            }
            catch
            {
                // Release the statement and rethrow the exception
                await _statement.ReleaseAsync(cancellationToken).ConfigureAwait(false);

                _statement = null;

                throw;
            }

            // Add this	command	to the active command list
            innerConn.AddPreparedCommand(this);
        }
        else
        {
            // Close statement for subsequently	executions
            await CloseAsync(cancellationToken).ConfigureAwait(false);
        }
    }