6

I'm a vb.net guy and have difficulty reading C#. I compiled the C# Dapper to a DLL and use it my app. My main concern is I think I need to modify the source to integrate by default the Transient Fault Handling Framework for SQL Azure in each SQL query.

I can add the retry logic on the connection level because it is ont top of dapper, but not at the execute query level which is embedded in drapper class.

Anyone has done that yet ?

* UPDATE *

Does using only ReliableSqlConnection on top of Dapper call will handle a retry logic on the execute non query ?

Here is sample code of retry from MS with the transietn fault hanling

using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.AzureStorage;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;
using System.Data;

...

using (ReliableSqlConnection conn = new ReliableSqlConnection(connString, retryPolicy))
{
conn.Open();

IDbCommand selectCommand = conn.CreateCommand();
selectCommand.CommandText = 
  "UPDATE Application SET [DateUpdated] = getdate()";

// Execute the above query using a retry-aware ExecuteCommand method which 
// will automatically retry if the query has failed (or connection was 
// dropped).
int recordsAffected = conn.ExecuteCommand(selectCommand, retryPolicy);

}

Here is the execute part of Dapper code, same name is used but I guess it is a custom execute function

    private static int ExecuteCommand(IDbConnection cnn, IDbTransaction transaction, string sql, Action<IDbCommand, object> paramReader, object obj, int? commandTimeout, CommandType? commandType)
    {
        IDbCommand cmd = null;
        bool wasClosed = cnn.State == ConnectionState.Closed;
        try
        {
            cmd = SetupCommand(cnn, transaction, sql, paramReader, obj, commandTimeout, commandType);
            if (wasClosed) cnn.Open();
            return cmd.ExecuteNonQuery();
        }
        finally
        {
            if (wasClosed) cnn.Close();
            if (cmd != null) cmd.Dispose();
        }
    }
5
  • I can confirm that I certainly haven't - although personally I would be expecting to implement transient fault handling around dapper rather than inside dapper. Commented Mar 13, 2013 at 7:44
  • Yes but around dapper would be effective for the connection itself, but the logic integrate also ExecuteReaderWithRetry, or NonQuery, etc. The whole with retry at the execute level has to be integrated to dapper if i understand well. Commented Mar 14, 2013 at 17:24
  • 1
    Yeah but you could just write a "do with retry" extension method... Commented Mar 14, 2013 at 17:41
  • Ok that's where my C# capabilities are too low to do that, and converting to VB.net via regular converter doesn't seems to work on this, do you know a very effective C# to VB.net service ? Commented Mar 14, 2013 at 18:51
  • 1
    I assume vb supports extension methods. If that is the case, you can write the extension method in your own project in vb. no translation necessary. Commented Mar 14, 2013 at 20:16

1 Answer 1

3

I would recommend wrapping the retry around Dapper, preferably by using the RetryPolicy.ExecuteAction method. That way both the OPEN call to the connection and the command itself will be retried using the TFH retry policy:

For example:

        SqlRetryPolicy.ExecuteAction(() =>
        {
            // Place Dapper ExecuteCommand here: e.g.
            ExecuteCommand(conn,  trans, ... )
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.