コード例 #1
0
        /// <summary>
        /// Immidiatelly executes all prepared actions of this transaction.
        /// If this method is not called, but transaction are disposed - all prepared actions will be performed later, in some scheduler.
        /// </summary>
        /// <param name="error">Output parameter containing error message, if some error occured</param>
        /// <returns>Result of performed transaction.</returns>
        public bool Commit(out string error)
        {
            error = null;

            if (_isCommited)
            {
                return(true);
            }

            _isCommited = true;

            return(RetryOnDeadlock(() => {
                var queue = _actions.ToArray();

                using (var connection = _factory.AdoNetRepository().GetConnection()) {
                    connection.Open();
                    using (var transaction = connection.BeginTransaction(GetDataIsolationLevel(_isolationLevel))) {
                        var args = new DbActionArgs(connection, transaction);

                        foreach (var action in queue)
                        {
                            if (!action(args))
                            {
                                _isCommited = false;
                                transaction.Rollback();
                                throw new Exception("Unknown error occured. Transaction is rolled back.");
                            }
                        }

                        transaction.Commit();
                    }
                }
            }, out error));
        }
コード例 #2
0
        protected override NpgsqlCommand CommandFor(string commandText, DbActionArgs args, IEnumerable <NpgsqlParameter> parameters, CommandType type = CommandType.StoredProcedure)
        {
            var connection  = args.Connection as NpgsqlConnection;
            var transaction = args.Transaction as NpgsqlTransaction;

            if (connection == null)
            {
                if (args.Connection == null)
                {
                    throw new InvalidOperationException($"{nameof(args.Connection)} is not instantiated!");
                }

                throw new InvalidOperationException($"{nameof(args.Connection)} has unexpected type {args.Connection.GetType()}!");
            }

            var command = transaction == null
                ? new NpgsqlCommand(commandText, connection)
            {
                CommandType = type
            }
                : new NpgsqlCommand(commandText, connection, transaction)
            {
                CommandType = type
            };

            foreach (var sqlParameter in parameters)
            {
                command.Parameters.Add(sqlParameter);
            }

            return(command);
        }
コード例 #3
0
 /// <summary>
 /// Вписывает таблицу в базу данных. Может кидать эксепшоны
 /// </summary>
 /// <exception cref="ArgumentException">Передается значение null или пустая строка (""), а таблица принадлежит коллекции. </exception>
 /// <exception cref="DuplicateNameException">Таблица принадлежит коллекции, которая уже содержит таблицу с таким же именем. (При сравнении учитывается регистр).</exception>
 /// <seealso cref="DataTableUtils.ToDataTable{T}(IEnumerable{T}, string[], string)"/>
 public static void BulkInsert([NotNull] this DataTable table, [NotNull] DbActionArgs args, SqlBulkCopyOptions options = SqlBulkCopyOptions.Default, int batchSize = 5000, int timeout = 1200)
 {
     using (var bulk = new SqlBulkCopy((SqlConnection)args.Connection, options, (SqlTransaction)args.Transaction)
     {
         DestinationTableName = table.TableName,
         BatchSize = batchSize,
         BulkCopyTimeout = timeout
     }) {
         bulk.WriteToServer(table);
     }
 }