How do you make SMO release it's connections?
I have this code:
public static class SqlServerConnectionFactory
{
public static Server GetSmoServer()
{
using (var c = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
{
var s = new ServerConnection(c);
c.Close();
return new Server(s);
}
}
public static Database GetSmoDatabase(Server server)
{
var db = server.Databases[ConfigurationManager.AppSettings["Database"]];
db.AutoClose = true;
return db;
}
}
Called like this from an ASP.Net MVC app run in IIS...:
public ActionResult Index()
{
server = SqlServerConnectionFactory.GetSmoServer();
database = SqlServerConnectionFactory.GetSmoDatabase(server);
var vm = new SettingsIndexViewmodel(database);
return View(vm);
}
For every call I make to this index method a connection is spun up - and is not released again.
So after 20 calls to the page, I have 20 of the connections awaiting command. This eventually ends up with an exception, when I cannot make new connections, because the connection pool is full.
What do I need to avoid this happening? I cannot seem to find a method on the SMO Server object like Dispose, close or similar.