示例#1
0
        /// <summary>
        /// Attempts to run the provided void method within a single database transaction
        /// </summary>
        /// <typeparam name="TArg1">The type of the first method argument</typeparam>
        /// <typeparam name="TArg2">The type of the second method argument</typeparam>
        /// <typeparam name="TArg3">The type of the third method argument</typeparam>
        /// <param name="method">The method which handles the transaction. This should be static!</param>
        /// <param name="saveSet"></param>
        /// <param name="arg1">The first method argument</param>
        /// <param name="arg2">The second method argument</param>
        /// <param name="arg3">The third method argument</param>
        /// <param name="numRetries">Number of times to attempt a retry if a serialisation error occurs</param>
        /// <returns>Returns whatever a successful "method" call returns</returns>
        protected void RunVoidQuery <TArg1, TArg2, TArg3>(Action <NpgsqlConnection, TArg1, TArg2, TArg3> method, TArg1 arg1,
                                                          TArg2 arg2, TArg3 arg3, int numRetries = DefaultNumDbRetries)
        {
            DecrementLifetime();
            var attempts = 0;

            while (true)
            {
                try
                {
                    using (var conn = PgDalConnection.GetConn())
                    {
                        using (var transaction = conn.BeginTransaction(IsolationLevel.Serializable /* changed */))
                        {
                            method(transaction.Connection, arg1, arg2, arg3);
                            transaction.Commit();
                            break;
                        }
                    }
                }
                catch (NpgsqlException e)
                {
                    if (!HandleNpgsqlException(e, ref attempts, numRetries))
                    {
                        throw;
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Attempts to run the provided method within a single database transaction
        /// </summary>
        /// <typeparam name="T">The return type of the provided method</typeparam>
        /// <param name="method">The method which handles the transaction. This should be static!</param>
        /// <param name="saveSet"></param>
        /// <param name="numRetries">Number of times to attempt a retry if a serialisation error occurs</param>
        /// <returns>Returns whatever a successful "method" call returns</returns>
        protected T RunReturningQuery <T>(Func <NpgsqlConnection, T> method, int numRetries = DefaultNumDbRetries)
        {
            DecrementLifetime();

            var attempts = 0;

            while (true)
            {
                try
                {
                    T ret;
                    using (var conn = PgDalConnection.GetConn())
                    {
                        using (var transaction = conn.BeginTransaction(IsolationLevel.Serializable /* changed */))
                        {
                            ret = method(transaction.Connection);
                            transaction.Commit();
                        }
                    }
                    return(ret);
                }
                catch (NpgsqlException e)
                {
                    if (!HandleNpgsqlException(e, ref attempts, numRetries))
                    {
                        throw;
                    }
                }
            }
        }