im using nhibernate as the orm for my webservices and i have implemented a CustomSqlDriver. The reason why i created CustomSqlDriver is because i need to get and save each query (with their parameters values) that gets executed in the app to the db. The AdjustCommand method gets called each time a query takes place (select,update,etc) and in the respective order.
public class CustomSqlDriver : SqlClientDriver {
private static readonly log4net.ILog log = log4net.LogManager.GetLogger("CustomSqlClientDriverLogger");
private static readonly Object _lock = new Object();
public override void AdjustCommand(IDbCommand command)
{
base.AdjustCommand(command);
var parameters = (SqlParameterCollection)command.Parameters;
QueryType queryType = GetQueryType(command);
string tableName = GetTableName(command.CommandText);
string fullCommandText = GetFullQuery(command, parameters);
Task.Run(() =>
{
lock (_lock)
{
log.Info($"Trying to save the query = {fullCommandText}");
SaveToDb((int)queryType, fullCommandText, tableName);
}
});
}
}
I need to save each query in order cause i will use them later in a different FIFO process. Cant do it in the same thread (cause it may be slow). As you can see, i tried using a lock on a static object within a Task.Run() and this works as long as the call to any webmethod is synchronous but if i call them in parallel, some rows lost order when they gets saved to the db.
Im not sure it this is the best approach D: Any help would be appreciated.