Пример #1
0
 /// <summary>
 /// Initializes a new <see cref="MySqlBatch"/> object, setting the <see cref="Connection"/> and <see cref="Transaction"/> if specified.
 /// </summary>
 /// <param name="connection">(Optional) The <see cref="MySqlConnection"/> to use.</param>
 /// <param name="transaction">(Optional) The <see cref="MySqlTransaction"/> to use.</param>
 public MySqlBatch(MySqlConnection?connection = null, MySqlTransaction?transaction = null)
 {
     Connection    = connection;
     Transaction   = transaction;
     BatchCommands = new MySqlBatchCommandCollection();
     m_commandId   = ICancellableCommandExtensions.GetNextId();
 }
Пример #2
0
 public static void DisConnectDB()
 {
     if (null == connection)
     {
         return;
     }
     connection.Close();
     connection = null;
 }
Пример #3
0
 public MySqlCommand(string?commandText, MySqlConnection?connection, MySqlTransaction?transaction)
 {
     GC.SuppressFinalize(this);
     m_commandId   = ICancellableCommandExtensions.GetNextId();
     m_commandText = commandText ?? "";
     Connection    = connection;
     Transaction   = transaction;
     CommandType   = CommandType.Text;
 }
Пример #4
0
 public static MySqlConnection ConnectDB()
 {
     if (null != connection)
     {
         return(connection);
     }
     connection = new MySqlConnection(ConnectionString);
     connection.Open();
     return(connection);
 }
Пример #5
0
    /// <summary>
    /// Executes the specified implementation asynchronously.
    /// </summary>
    /// <param name="executionToken">The execution token.</param>
    /// <param name="implementation">The implementation.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    /// <param name="state">The state.</param>
    /// <returns>The caller is expected to use the StreamingCommandCompletionToken to close any lingering connections and fire appropriate events.</returns>
    /// <exception cref="System.NotImplementedException"></exception>
    public override async Task <StreamingCommandCompletionToken> ExecuteStreamAsync(CommandExecutionToken <MySqlCommand, MySqlParameter> executionToken, StreamingCommandImplementationAsync <MySqlCommand> implementation, CancellationToken cancellationToken, object?state)
    {
        if (executionToken == null)
        {
            throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
        }
        if (implementation == null)
        {
            throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
        }

        var startTime = DateTimeOffset.Now;

        OnExecutionStarted(executionToken, startTime, state);

        MySqlConnection?con = null;

        try
        {
            con = await CreateConnectionAsync(cancellationToken).ConfigureAwait(false);

            var cmd = new MySqlCommand();

            cmd.Connection = con;
            executionToken.PopulateCommand(cmd, DefaultCommandTimeout);

            await implementation(cmd).ConfigureAwait(false);

            return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd, con));
        }
        catch (Exception ex)
        {
            if (con != null)
            {
                await con.DisposeAsync().ConfigureAwait(false);
            }

            if (cancellationToken.IsCancellationRequested) //convert Exception into a OperationCanceledException
            {
                var ex2 = new OperationCanceledException("Operation was canceled.", ex, cancellationToken);
                OnExecutionCanceled(executionToken, startTime, DateTimeOffset.Now, state);
                throw ex2;
            }
            else
            {
                OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
                throw;
            }
        }
    }
Пример #6
0
        public async Task RollbackAsync(IDbTransaction transaction)
        {
            MySqlTransaction mySqlTransaction = (MySqlTransaction)transaction;

            MySqlConnection?connection = mySqlTransaction.Connection;

            try
            {
                await mySqlTransaction.RollbackAsync().ConfigureAwait(false);
            }
            finally
            {
                if (connection != null)
                {
                    await connection.CloseAsync().ConfigureAwait(false);
                }
            }
        }
Пример #7
0
    /// <summary>
    /// Executes the specified implementation.
    /// </summary>
    /// <param name="executionToken">The execution token.</param>
    /// <param name="implementation">The implementation.</param>
    /// <param name="state">The state.</param>
    /// <returns>The caller is expected to use the StreamingCommandCompletionToken to close any lingering connections and fire appropriate events.</returns>
    /// <exception cref="System.NotImplementedException"></exception>
    public override StreamingCommandCompletionToken ExecuteStream(CommandExecutionToken <MySqlCommand, MySqlParameter> executionToken, StreamingCommandImplementation <MySqlCommand> implementation, object?state)
    {
        if (executionToken == null)
        {
            throw new ArgumentNullException(nameof(executionToken), $"{nameof(executionToken)} is null.");
        }
        if (implementation == null)
        {
            throw new ArgumentNullException(nameof(implementation), $"{nameof(implementation)} is null.");
        }

        var startTime = DateTimeOffset.Now;

        OnExecutionStarted(executionToken, startTime, state);

        MySqlConnection?con = null;

        try
        {
            con = CreateConnection();

            var cmd = new MySqlCommand();

            cmd.Connection = con;
            executionToken.PopulateCommand(cmd, DefaultCommandTimeout);

            implementation(cmd);

            return(new StreamingCommandCompletionToken(this, executionToken, startTime, state, cmd, con));
        }
        catch (Exception ex)
        {
            con?.Dispose();
            OnExecutionError(executionToken, startTime, DateTimeOffset.Now, ex, state);
            throw;
        }
    }
Пример #8
0
 public MySqlCommand(string?commandText, MySqlConnection?connection)
     : this(commandText, connection, null)
 {
 }
Пример #9
0
 public MySqlCommand(MySqlConnection?connection, MySqlTransaction?transaction)
     : this(null, connection, transaction)
 {
 }
 public SessionResetTask(ServerSession session, Task <bool> resetTask, MySqlConnection?owningConnection)
 {
     Session          = session;
     ResetTask        = resetTask;
     OwningConnection = owningConnection;
 }
 public static void AddSession(ServerSession session, MySqlConnection?owningConnection)
 {
     var resetTask = session.TryResetConnectionAsync(session.Pool !.ConnectionSettings, IOBehavior.Asynchronous, default);
Пример #12
0
 public MySqlDatabaseFixes(MySqlConnection?connection, AppSettings appSettings)
 {
     _connection  = connection;
     _appSettings = appSettings;
 }