The underlying provider failed on Open

2020-05-03 12:09发布

问题:

I made 3 Ajax processes to run the below code at the same time. but one of the processes throw exception that message says "The underlying provider failed on Open."

try{
    orderRepository orderRepo = new orderRepository(); // get context (Mysql)

    var result = (from x in orderRepo.orders
          where x.orderid == orderno
          select new {x.tracking, x.status, x.charged }).SingleOrDefault();

    charged = result.charged;
}catch(Exception e){
    log.Error(e.Message); //    The underlying provider failed on Open.
} 

And, I run the 1 Ajax call that failed before, then It passes through.

It happen to 1 of 3 (Ajax) process, sometimes, 2 of 5 process.

I guess it because all process try to using Database same time. but I couldn't find the solution.

This is my connection string,

<add name="EFMysqlContext" connectionString="server=10.0.0.10;User Id=root;pwd=xxxx;Persist Security Info=True;database=shop_db" providerName="Mysql.Data.MySqlClient" />

Anybody know the solution or something I can try, please advise me.

Thanks

回答1:

It sounds like a problem because of concurrent connection with SQL Server using same username. Have you tried destroying/disposing the repository(or connection) object after using it? Give it a try:

 try{
       using( orderRepository orderRepo = new orderRepository()) // get context (Mysql)
        {
          var result = (from x in orderRepo.orders
              where x.orderid == orderno
              select new {x.tracking, x.status, x.charged }).SingleOrDefault();

        charged = result.charged;
    } // orderRepo object automatically gets disposed here
catch(Exception e){
        log.Error(e.Message); //    The underlying provider failed on Open.
    } }


回答2:

Not sure if it matters, but your provider name is Mysql.Data.MySqlClient and not MySql.Data.MySqlClient (if it is case-sensitive, this could be the cause).