question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataContext Close

See original GitHub issue

If I run this Code

{
var db = new MyContext();
var x = db.SomeTable.ToList();
var y = db.SomeTable.ToList();
}

until context end i have 2 connections open (sleeping) on db, after context ends they are released.

but if i ran this code

{
var db = new MyContext();
db.OnTraceConnection = (e) =>
{
         if (e.TraceInfoStep == TraceInfoStep.Completed || e.TraceInfoStep == TraceInfoStep.Error)
                    e.DataConnection.Close();
};
var x = db.SomeTable.ToList();
var y = db.SomeTable.ToList();
}

until context end i have 1 connections open(sleeping) on db, after context ends this is released.

Environment details

linq2db version: 2.9.0 Database Server: Microsoft Sql Server Database Provider: SqlServerVersion.v2017 Operating system: Windows .NET Framework: 4.7.2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Sep 20, 2019

@croban, fix prepared. I hope it will be released next Thursday.

0reactions
sdanylivcommented, Sep 13, 2019

Also it is reproducible only for async version. Investigation needed.

[Test]
public void DataContextTest([IncludeDataSources(false, TestProvName.AllSqlServer)] string context)
{
	var db = new DataContext(context);
	for (int i = 0; i < 1000; i++)
	{
		var items1 = db.GetTable<Child>().ToArray();
	}
}

[Test]
public async Task DataContextTestAsync([IncludeDataSources(false, TestProvName.AllSqlServer)] string context)
{
	var db = new DataContext(context);
	for (int i = 0; i < 1000; i++)
	{
		var items1 = await db.GetTable<Child>().ToArrayAsync();
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Does Linq to Sql close connections automatically? [duplicate]
No, the first version does not close the connection until the application context is disposed of (that is, when the application terminates). The ......
Read more >
Connection management - EF6
But, regardless of that setting, the store connection is always closed when the context is disposed. So if you have more than one...
Read more >
Method to re-open a connection on a datacontext if it has ...
The following method is used to keep a DataContext open if it is closed or broken and is called before most calls to...
Read more >
[.NET development] How to automatically close your contexts
The first step is to implement the interface IDisposable and its “Dispose” method. In this method you just Dispose() your DbContext which will ......
Read more >
How does Entity Framework know when to close ...
It appears that the DBContext will usually auto-close the connections as needed. That said, there are other problems with reusing an EF ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found