linq2db / linq2db.LINQPad

linq2db.LINQPad is a driver for LINQPad.
MIT License
74 stars 23 forks source link

Can't close connection #94

Closed asherber closed 11 months ago

asherber commented 11 months ago

Using the default LINQ to SQL driver for Sql Server, the Connection object in a LinqPad script starts out in a closed state and can be opened and closed as needed. But using the Linq2db driver (for a Postgres database), Connection starts out in an open state -- and it appears that calling Close() has no effect.

Connection.State.Dump();   // Open
Connection.Close();
Connection.State.Dump();   // Open

Is there a way to either get the connection to start closed, or to close it by hand?

MaceWindu commented 11 months ago

It looks like LinqPAD behavior to me.

All we do is create and return connection instance to LinqPAD here

I've made some tests:

Connection.GetHashCode().Dump();
Connection.State.Dump();
Connection.Close();
Connection.State.Dump();
Connection.GetHashCode().Dump();
54897010
Open
Open
54897010

if I save connection to variable to avoid Connection property access I get expected behavior:

var cn = Connection;
cn.GetHashCode().Dump();
cn.State.Dump();
cn.Close();
cn.State.Dump();
cn.GetHashCode().Dump();
21201098
Open
Closed
21201098

I suspect Connection property implementation by LinqPAD opens connection if it is not opened on invoke. I would recommend to ask on LinqPAD forum if it is an issue for you as I don't see currently how we can affect this behavior from driver

asherber commented 11 months ago

Thanks, that's interesting. I assumed it was a driver issue because the behavior is different if I use the LINQ to SQL driver to connect SQL Server, or if I use Npgsql directly.