SMO.Restore.SqlRestore sometimes throws a timeout exception on deployed computers

I have an application used to manage databases to demonstrate our software, one of the things it does is to get a copy of the database from a central server and restore it to a local SQL instance. Everything works correctly on the backup part, but during recovery, some people report that they receive the next exception in the middle of recovery.

Microsoft.SqlServer.Management.Smo.FailedOperationException: Restore failed for Server 'Computername'.
---> Microsoft.SqlServer.Management.Common.ExecutionFailureException: 
     An exception occurred while executing a Transact-SQL statement or batch. 
     ---> System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
          at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
          (snip)
          at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
      --- End of inner exception stack trace ---
      at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
      (snip)
      at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv)
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv)
at ContractFlowTool.WebInfinity2.AttachDatabase.RestoreLocal(AttachDatabaseArgs arg)

MSDN is lightweight enough for the internal workings of SMO classes. I could not find any way to change the timeout for the restore. What can I do, exception will not happen?


Here is the code that performs the recovery

private static bool RestoreLocal(AttachDatabaseArgs arg)
{
    if (arg.DestDatabase == null)
        throw new ArgumentNullException("DestDatabase");
    SqlConnectionInfo serverConnInfo = new SqlConnectionInfo(/*snip*/);
    ServerConnection serverConn = null;
    serverConn = new ServerConnection(serverConnInfo);
    var remoteServer = new Server(serverConn);
    var clinicFolder = ClinicFolder(arg);
    var restore = new Restore();
    restore.PercentCompleteNotification = 5;
    restore.NoRecovery = false;
    restore.RelocateFiles.Add(/*snip mdf*/);
    restore.RelocateFiles.Add(/*snip ldf*/);
    restore.Database = arg.LocalDB;
    restore.ReplaceDatabase = true;
    restore.Action = RestoreActionType.Database;
    restore.PercentComplete += arg.ProgressForm.Restore_PercentComplete;

    restore.SqlRestore(remoteServer);
}
+5
2

, ServerConnection.StatementTimeout . -, , 3 .

(...) 
serverConn = new ServerConnection(serverConnInfo);
serverConn.StatementTimeout = 240; //<- set this.
var remoteServer = new Server(serverConn);
var clinicFolder = ClinicFolder(arg);
(...)
+8

U , , 2 .

(SqlConnection1)

Dim sqlStmt As String = String.Format( "BACKUP DATABASE map TO DISK = '{0}'", backup_directory + backupfile)

            Using bu2 As New SqlCommand(sqlStmt, SqlConnection1)
                SqlConnection1.Open()
                bu2.CommandTimeout = 180   //this line is the key
                bu2.ExecuteNonQuery()
                SqlConnection1.Close()
            End Using
        End Using
0

All Articles