Connection Pooling and connection leak
could following cause our application leak connections?
{
//do stuff
var myrecord = context.mytable.where(x=> x.id == 1).singleordefault();
//do more stuff
}
protected myentities getmyentityconnection()
{
var context = new myentities(getpreparedconnection(fswellknownconnection.mydb));
context.commandtimeout = constants.entity_connection_timeout;
return context;
}
public virtual entityconnection getpreparedconnection(fswellknownconnection connection)
{
entityconnection con = null;
try
{
con = new entityconnection(getconnectionstring(connection));
con.open();
using (var com = con.storeconnection.createcommand())
{
com.commandtype = system.data.commandtype.text;
com.commandtext = "set arithabort on;";
com.executenonquery();
}
}
catch
{
if (con != null)
{
con.dispose();
con = null;
}
throw;
}
return con;
}
the usual pattern is to let ef open/close underlying connection needed.
here :
- getpreparedconnection returns open connection
- getmyentityconnection uses connection return apparently ef context
- context disposed
here suspect provided open connection, ef assume it's responsability close connection. happens if don't set arithabort on , provide closed connection ? see happens.
if fixes issue turn finding best option set artihabort on. if possible set option @ database level going using ef usual.
another option other waty round, rather provide ef own connection implement this, use connection provided ef, possibly using sqlserverconnection.statechanged, automatically send command when ef opens connection ? never tried worth check approach.
or yet option inherit context (is dbcontext ?) can implement part of context class making transparent client code.
please mark whatever response solved issue thread marked "answered".
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment