mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 17:23:27 -05:00
Ensure connection open for OE queries and fix connection disposal (#359)
* Ensure connection open for OE queries and fix connection disposal - Dispose connection in Metadata service, to ensure we cleanly dispose and don't rely on garbage colleciton - Fixed issue where if the connection was closed, expanding databases in the Server would fail. This is because SMO doesn't always reopen the connection, certainly not for Server level queries. The solution is to always check if open and reopen. - Added unit tests for this, which required mocking the relevant IsOpen / OpenConnection methods. Refactored SMO wrapper calls into a dedicated class file to handle this
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlServer.Management.Common;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlTools.Extensibility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
|
||||
using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Internal for testing purposes only. This class provides wrapper functionality
|
||||
/// over SMO objects in order to facilitate unit testing
|
||||
/// </summary>
|
||||
internal class SmoWrapper
|
||||
{
|
||||
public virtual Server CreateServer(SqlConnection connection)
|
||||
{
|
||||
ServerConnection serverConn = new ServerConnection(connection);
|
||||
return new Server(serverConn);
|
||||
}
|
||||
|
||||
public virtual bool IsConnectionOpen(SmoObjectBase smoObj)
|
||||
{
|
||||
SqlSmoObject sqlObj = smoObj as SqlSmoObject;
|
||||
return sqlObj != null
|
||||
&& sqlObj.ExecutionManager != null
|
||||
&& sqlObj.ExecutionManager.ConnectionContext != null
|
||||
&& sqlObj.ExecutionManager.ConnectionContext.IsOpen;
|
||||
}
|
||||
|
||||
public virtual void OpenConnection(SmoObjectBase smoObj)
|
||||
{
|
||||
SqlSmoObject sqlObj = smoObj as SqlSmoObject;
|
||||
if (sqlObj != null
|
||||
&& sqlObj.ExecutionManager != null
|
||||
&& sqlObj.ExecutionManager.ConnectionContext != null)
|
||||
{
|
||||
sqlObj.ExecutionManager.ConnectionContext.Connect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user