LINQ: DataContext Life and System.Data.SqlClient.SqlException: timed out

I seem to get a lot of things in my Linq 2 SQL

System.Data.SqlClient.SqlException: Timed out. The wait period expires before the operation is completed or the server is not responding.

There is no reason for this; it is a simple query that returns 1 record.

I was thinking about opening my datacontext in a Using statement for every method that he needs, I am currently using a private module level variable to open the datacontext file.

Is this recommended?

I don’t understand why it will be time, I can only think that I have too many data contexts around ....

Any ideas?

+3
source share
2 answers

Datacontext is designed to open when you use it and then throw it away. For example:

using (var dc = new MyDataContext())
    dc.sp_DailyJob();

Immediately after the block, the usingdatacontext returns back to the connection pool.

Now, if you cache the DataContext in a local module, it may not work for a while. Then SQL Server will eventually close your connection because it has been idle for too long. The next call generates a message Timeout expired.

I would remove the caching DataContext and continue to create a new DataContext for each query or query you run. Dispose of them as soon as you can. There is no overhead because connections are cached using an optimized connection pool.

+1
source

( DataContext Linq to SQL) Linq 2 SQL ... , :)

+2

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


All Articles