A clean way to retry failed System.Data.SqlClient actions

I inherited a fairly large C # code, which is clogged hundreds of times with the following way to perform operations with the database:

using (SqlConnection connection = new SqlConnection(connectionString)) {
    SqlCommand command = new SqlCommand(queryString, connection);
    command.Connection.Open();
    command.ExecuteNonQuery();
}

However, due to factors beyond my control, the database server is cruel and especially error prone, so a significant portion of these requests fail. Given that this particular way of accessing the database is scattered throughout the code, how can I encode the replay in one place and apply it everywhere SqlCommand?

Some thoughts:

  • I tried to override SqlConnection/ SqlCommand ExecuteNonQueryto create a wrapper version. Not good. SqlConnectionand SqlCommandare C # private objects.
  • I could create a container class with a name MySqlConnectionthat contains a .NET object SqlConnection, and then create my own ExecuteNonQuery, which in turn will retry several times SqlCommand.ExecuteNonQuery(). However, this is not good, because I would have to implement each function SqlCommandAND train other people to directly use my wrapper class instead SqlCommand. Unfortunately, user training is not an acceptable solution. I am stuck with other developers who have outsourced, have frequent turnover and never heed my requests to use the new SQL class.
  • As I mentioned earlier, I simply do not have access (politically and technically) to the dirty DB layer. The root cause of the problem is simply a poor SQL server setup, but I cannot solve the problem at this level.

, ?

+3
3

, 2x4 ,

- postsharp (http://www.sharpcrafters.com/) . , . SqlCommand; disposed, info state change - , .

, , , . , , , .

SQL, , SQL-.

+3

, , , ? , , , . .

..... ... ...

, ?

0

, - "-".

( ). -, , ( ). - , . , ( ..).

Just saying it. This will be a way to implement repetitions without changing the code.

0
source

Source: https://habr.com/ru/post/1768324/


All Articles