Page 1 of 1

Error on opening db connection (MultiThread app)

Posted: Fri 03 May 2013 04:47
by idem84
I start two threads, (linqconnect model), but it throws an exception "error on opening db connection"
No ex throws if i remove the two threads, and if I run one method after the other, but i want to optimize to reduce the time of the tasks trough the threads
Support LinqConnect multithreadding? Or i have to create another context or model?

mymethod()
{
_mainBackupGpsRawDataThread.Start();
_mainBackupGpsHistoricDataThread.Start();
}

code in thread1:
var total = MHistoryContext.Gpsrawdatas.Count(x => x.GpsLocalDate < toDateToBackup1);
...
code in thread2 (here throws ex) :
var total = MHistoryContext.Gpshistorics.Count(x => x.GpsLocalDate < toDateToBakup2);
...

Im using dotConnect for MySQL 7.6.226 (LinqConnect), VS2012.2, Win8x64

Thanks in advance!

Re: Error on opening db connection (MultiThread app)

Posted: Wed 08 May 2013 10:59
by MariiaI
Please specify the stack trace of the "Error on opening db connection" exception. Also, if possible, please send us a sample project, so that we could investigate this issue in more details.

In fact, this issue is most likely related to the situation when one DataContext object is accessed from different threads simultaneously.
DataContext objects are not thread-safe, at least because a DataContext instance uses a single connection, which is not thread-safe itself. Due to the fact that likelihood of trying to access the DataContext on several different threads is high, we don't recommend using a static instance of DataContext. DataContext is a lightweight object, and it is recommended to use a new DataContext for each unit of work instead of keeping it for a long time.