mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-20 01:25:41 -05:00
Fixing the bug with connections on database make restore fail (#473)
* closing the connections that don't need to be open and keeping track of the connections that should stay open
This commit is contained in:
@@ -313,6 +313,17 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
{
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (this.serverConnection != null)
|
||||
{
|
||||
this.serverConnection.Disconnect();
|
||||
}
|
||||
if(this.dataContainer != null)
|
||||
{
|
||||
this.dataContainer.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -142,13 +142,17 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
|
||||
if (connInfo != null)
|
||||
{
|
||||
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(connInfo, databaseExists: true);
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo);
|
||||
if (sqlConn != null && !connInfo.IsSqlDW && !connInfo.IsAzure)
|
||||
using (DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(connInfo, databaseExists: true))
|
||||
{
|
||||
BackupConfigInfo backupConfigInfo = this.GetBackupConfigInfo(helper.DataContainer, sqlConn, sqlConn.Database);
|
||||
backupConfigInfo.DatabaseInfo = AdminService.GetDatabaseInfo(connInfo);
|
||||
response.BackupConfigInfo = backupConfigInfo;
|
||||
using (SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo))
|
||||
{
|
||||
if (sqlConn != null && !connInfo.IsSqlDW && !connInfo.IsAzure)
|
||||
{
|
||||
BackupConfigInfo backupConfigInfo = this.GetBackupConfigInfo(helper.DataContainer, sqlConn, sqlConn.Database);
|
||||
backupConfigInfo.DatabaseInfo = AdminService.GetDatabaseInfo(connInfo);
|
||||
response.BackupConfigInfo = backupConfigInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,7 +237,6 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
RequestContext<RestoreResponse> requestContext)
|
||||
{
|
||||
RestoreResponse response = new RestoreResponse();
|
||||
|
||||
try
|
||||
{
|
||||
ConnectionInfo connInfo;
|
||||
@@ -243,10 +246,12 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
{
|
||||
try
|
||||
{
|
||||
RestoreDatabaseTaskDataObject restoreDataObject = this.restoreDatabaseService.CreateRestoreDatabaseTaskDataObject(restoreParams);
|
||||
|
||||
RestoreDatabaseTaskDataObject restoreDataObject = this.restoreDatabaseService.CreateRestoreDatabaseTaskDataObject(restoreParams, connInfo);
|
||||
|
||||
if (restoreDataObject != null)
|
||||
{
|
||||
restoreDataObject.LockedDatabaseManager = ConnectionServiceInstance.LockedDatabaseManager;
|
||||
// create task metadata
|
||||
TaskMetadata metadata = TaskMetadata.Create(restoreParams, SR.RestoreTaskName, restoreDataObject, ConnectionServiceInstance);
|
||||
metadata.DatabaseName = restoreParams.TargetDatabaseName;
|
||||
@@ -297,6 +302,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
{
|
||||
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(connInfo, databaseExists: true);
|
||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo);
|
||||
// Connection gets discounnected when backup is done
|
||||
|
||||
BackupOperation backupOperation = CreateBackupOperation(helper.DataContainer, sqlConn, backupParams.BackupInfo);
|
||||
SqlTask sqlTask = null;
|
||||
@@ -332,17 +338,19 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
|
||||
if (connInfo != null)
|
||||
{
|
||||
sqlConn = ConnectionService.OpenSqlConnection(connInfo);
|
||||
if (sqlConn != null && !connInfo.IsSqlDW && !connInfo.IsAzure)
|
||||
using (sqlConn = ConnectionService.OpenSqlConnection(connInfo))
|
||||
{
|
||||
connectionInfo = connInfo;
|
||||
return true;
|
||||
if (sqlConn != null && !connInfo.IsSqlDW && !connInfo.IsAzure)
|
||||
{
|
||||
connectionInfo = connInfo;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if(sqlConn != null)
|
||||
if(sqlConn != null && sqlConn.State == System.Data.ConnectionState.Open)
|
||||
{
|
||||
sqlConn.Close();
|
||||
}
|
||||
|
||||
@@ -88,10 +88,6 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery.RestoreOperation
|
||||
RestoreAsFileName = x.PhysicalNameRelocate
|
||||
});
|
||||
response.CanRestore = CanRestore(restoreDataObject);
|
||||
if (!response.CanRestore)
|
||||
{
|
||||
response.ErrorMessage = SR.NoBackupsetsToRestore;
|
||||
}
|
||||
|
||||
response.PlanDetails.Add(LastBackupTaken,
|
||||
RestorePlanDetailInfo.Create(name: LastBackupTaken, currentValue: restoreDataObject.GetLastBackupTaken(), isReadOnly: true));
|
||||
@@ -150,7 +146,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery.RestoreOperation
|
||||
/// </summary>
|
||||
/// <param name="restoreParams">Restore request parameters</param>
|
||||
/// <returns>Restore task object</returns>
|
||||
public RestoreDatabaseTaskDataObject CreateRestoreDatabaseTaskDataObject(RestoreParams restoreParams)
|
||||
public RestoreDatabaseTaskDataObject CreateRestoreDatabaseTaskDataObject(RestoreParams restoreParams, ConnectionInfo connectionInfo = null)
|
||||
{
|
||||
RestoreDatabaseTaskDataObject restoreTaskObject = null;
|
||||
string sessionId = string.IsNullOrWhiteSpace(restoreParams.SessionId) ? Guid.NewGuid().ToString() : restoreParams.SessionId;
|
||||
@@ -161,6 +157,10 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery.RestoreOperation
|
||||
}
|
||||
restoreTaskObject.SessionId = sessionId;
|
||||
restoreTaskObject.RestoreParams = restoreParams;
|
||||
if (connectionInfo != null)
|
||||
{
|
||||
restoreTaskObject.ConnectionInfo = connectionInfo;
|
||||
}
|
||||
|
||||
return restoreTaskObject;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ using Microsoft.SqlTools.ServiceLayer.DisasterRecovery.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery.RestoreOperation
|
||||
{
|
||||
@@ -65,7 +66,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery.RestoreOperation
|
||||
/// <summary>
|
||||
/// Includes the plan with all the data required to do a restore operation on server
|
||||
/// </summary>
|
||||
public class RestoreDatabaseTaskDataObject : SmoScriptableTaskOperation, IRestoreDatabaseTaskDataObject
|
||||
public class RestoreDatabaseTaskDataObject : SmoScriptableOperationWithFullDbAccess, IRestoreDatabaseTaskDataObject
|
||||
{
|
||||
|
||||
private const char BackupMediaNameSeparator = ',';
|
||||
@@ -266,29 +267,66 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery.RestoreOperation
|
||||
base.Execute(mode);
|
||||
}
|
||||
|
||||
public ConnectionInfo ConnectionInfo { get; set; }
|
||||
|
||||
public override string ServerName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.ConnectionInfo != null)
|
||||
{
|
||||
return this.ConnectionInfo.ConnectionDetails.ServerName;
|
||||
}
|
||||
|
||||
return this.Server.Name;
|
||||
}
|
||||
}
|
||||
|
||||
public override string DatabaseName
|
||||
{
|
||||
get
|
||||
{
|
||||
return TargetDatabaseName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the restore operations
|
||||
/// </summary>
|
||||
public override void Execute()
|
||||
{
|
||||
if (IsValid && RestorePlan.RestoreOperations != null && RestorePlan.RestoreOperations.Any())
|
||||
try
|
||||
{
|
||||
// Restore Plan should be already created and updated at this point
|
||||
|
||||
RestorePlan restorePlan = GetRestorePlanForExecutionAndScript();
|
||||
|
||||
if (restorePlan != null && restorePlan.RestoreOperations.Count > 0)
|
||||
if (IsValid && RestorePlan.RestoreOperations != null && RestorePlan.RestoreOperations.Any())
|
||||
{
|
||||
restorePlan.PercentComplete += (object sender, PercentCompleteEventArgs e) =>
|
||||
// Restore Plan should be already created and updated at this point
|
||||
|
||||
RestorePlan restorePlan = GetRestorePlanForExecutionAndScript();
|
||||
|
||||
if (restorePlan != null && restorePlan.RestoreOperations.Count > 0)
|
||||
{
|
||||
OnMessageAdded(new TaskMessage { Description = $"{e.Percent}%", Status = SqlTaskStatus.InProgress });
|
||||
};
|
||||
restorePlan.Execute();
|
||||
restorePlan.PercentComplete += (object sender, PercentCompleteEventArgs e) =>
|
||||
{
|
||||
OnMessageAdded(new TaskMessage { Description = $"{e.Percent}%", Status = SqlTaskStatus.InProgress });
|
||||
};
|
||||
restorePlan.Execute();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException(SR.RestoreNotSupported);
|
||||
}
|
||||
}
|
||||
else
|
||||
catch(Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException(SR.RestoreNotSupported);
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (this.Server.ConnectionContext.IsOpen)
|
||||
{
|
||||
this.Server.ConnectionContext.Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user