Wednesday, July 31, 2013

Connection Pooling and connection leak

Could the following cause our application to leak connections?




using (var context = GetMyEntityConnection())

{

//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;

}

No comments:

Post a Comment