0

В многопотоке заношу данные в БД. В какой то момент выдаёт ошибку.

CONN_STRING = "server=localhost;user=newuser;database=xxx;password=xxx;default command timeout=1000;";
using (MySqlConnection con = new MySqlConnection(CONN_STRING ))
{
    con.Open();
    
    var sql = $"INSERT INTO ....)";

    using (MySqlCommand cmd = new MySqlCommand(sql, con))
    {
        cmd.CommandTimeout = 60;
        cmd.ExecuteNonQuery();
    }
}

Unhandled exception. System.AggregateException: One or more errors occurred. (error connecting: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.) (error connecting: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.) (error connecting: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.) ---> MySql.Data.MySqlClient.MySqlException (0x80004005): error connecting: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

Настройки mysql

[mysqld]

wait_timeout=600
interactive_timeout = 600
key_buffer_size         = 16M
max_allowed_packet      = 16000M
thread_stack            = 192K
thread_cache_size       = 8
myisam-recover-options  = BACKUP
max_connections        = 1500
4
  • cmd.CommandTimeout = 60; - если убрать эту строку, то проблема останется? 16 июл 2020 в 10:45
  • @AndreiKhotko да, остается
    – Radzhab
    16 июл 2020 в 10:47
  • Кое-что нашел. Может, это вам поможет? ссылка 16 июл 2020 в 10:56
  • Судя по ошибке, вы запускаете одновременно потоков больше, чем размер пула соединений. Как вариант можно вручную (с помощью, например, SemaphorSlim) ограничивать количество потоков не более размера пула. 16 июл 2020 в 15:55

2 ответа 2

0

Попробуйте вызывать явно

con.Close();

когда освободится.

4
  • А куда именно, Азиз
    – Radzhab
    16 июл 2020 в 10:32
  • Он разве не должен автоматом закрывать?
    – Radzhab
    16 июл 2020 в 10:32
  • После команды явно вызовите. 16 июл 2020 в 10:54
  • using вызывает Dispose, внутри которого вызывается Close. Так что это не поможет. 16 июл 2020 в 15:59
0

Удалил MySQL.Data.dll. Поставил пакет https://www.nuget.org/packages/MySqlConnector/ и без единой правки кода все заработало. Только using изменил - а код не трогал вообще

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.