static internal void TryOpenConnection(IDbConnection connection) { // This is the retry loop, handling the retries session // is done in the catch for performance reasons // RetryAmount + 1 because it's initial attempt + RetryAmount (5) for (int attempt = 0; attempt < RetryAmount + 1; attempt++) { try { if (attempt > 0) { SyncServiceTracer.TraceInfo("Retrying opening connection, attempt {0} of {1}.", attempt, RetryAmount); } // Open the connection connection.Open(); // Test the open connection SqlConnection sqlConn = connection as SqlConnection; if (sqlConn != null) { using (SqlCommand sqlCommand = new SqlCommand("Select 1", sqlConn)) { sqlCommand.ExecuteScalar(); // Connection is valid, return successful return; } } else { //Don't test the injected connection return; } } catch (SqlException sqlException) { // Throw Error if we have reach the maximum number of retries if (attempt == RetryAmount) { SyncServiceTracer.TraceError("Open connection failed after max retry attempts, due to exception: {0}", sqlException.Message); throw; } // Determine if we should retry or abort. if (!RetryLitmus(sqlException)) { SyncServiceTracer.TraceError("Open connection failed on attempt {0} of {1}, due to unretryable exception: {2}", attempt + 1, RetryAmount, sqlException.Message); throw; } else { SyncServiceTracer.TraceWarning("Open connection failed on attempt {0} of {1}, due to retryable exception: {2}", attempt + 1, RetryAmount, sqlException.Message); // Backoff Throttling Thread.Sleep(RetryWaitMilliseconds * (int)Math.Pow(2, attempt)); } } } }
private static void CleanupBatchDirectory(string path, string headerFilePath, BatchHeader header) { try { if (Directory.Exists(path)) { // First delete all the batch related files. foreach (var batchFileName in header.BatchFileNames) { string filePath = Path.Combine(path, batchFileName); if (File.Exists(filePath)) { File.Delete(filePath); } } // Delete the header file. if (File.Exists(headerFilePath)) { File.Delete(headerFilePath); } // If there are no other files in the directory then delete the directory. if (0 == Directory.GetFiles(path).Length) { Directory.Delete(path, true); } } } catch (Exception exception) { SyncServiceTracer.TraceWarning("Error cleaning up batch directory. " + WebUtil.GetExceptionMessage(exception)); } }
/// <summary> /// Returns the edition of the SQL Server to which connection object is connected /// </summary> public static SqlEdition GetEdition(SqlConnection connection, SqlTransaction transaction) { WebUtil.CheckArgumentNull(connection, "connection"); bool openedConnection = OpenConnection(connection); SqlCommand cmd = null; SqlDataReader reader = null; SqlEdition edition; try { cmd = new SqlCommand(ServerPropertyQuery, connection, transaction); reader = cmd.ExecuteReader(); reader.Read(); string editionStr = reader.GetString(0); string versionStr = reader.GetString(1); if (editionStr.Equals("SQL Azure")) { edition = SqlEdition.SqlAzure; } else { String[] versionParts = versionStr.Split('.'); int majorVersion = Int32.Parse(versionParts[0], CultureInfo.InvariantCulture); if (majorVersion == 9) { edition = SqlEdition.Sql2005; } else if (majorVersion >= 10) { edition = SqlEdition.Sql2008; } else { throw SyncServiceException.CreateInternalServerError(String.Format("Unsupported Sql Edition {0}", versionStr)); } } SyncServiceTracer.TraceInfo("Version of connection detected to be {0}", edition); } finally { if (reader != null) { reader.Dispose(); } if (cmd != null) { cmd.Dispose(); } if (openedConnection) { connection.Close(); } } return(edition); }
static internal bool OpenConnection(IDbConnection connection) { WebUtil.CheckArgumentNull(connection, "connection"); bool openedConnection = false; switch (connection.State) { case ConnectionState.Open: break; case ConnectionState.Broken: SyncServiceTracer.TraceVerbose("Closing broken connection"); connection.Close(); goto case ConnectionState.Closed; case ConnectionState.Closed: if (connection is System.Data.SqlClient.SqlConnection) { // Blank out the password System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder.ConnectionString = connection.ConnectionString; if (!String.IsNullOrEmpty(builder.Password)) { builder.Password = "******"; } SyncServiceTracer.TraceVerbose("Connecting using string: {0}", builder.ConnectionString); } else { SyncServiceTracer.TraceVerbose("Connecting to database: {0}", connection.Database); } // Check for SqlConnection if (connection is SqlConnection) { TryOpenConnection(connection); } else { connection.Open(); } openedConnection = true; break; default: throw SyncServiceException.CreateInternalServerError(String.Format("Unhandled ConnectionState {0}", connection.State)); } return(openedConnection); }