Search code examples
.netactive-directorydynamics-crmdynamics-crm-4

Import organization with OptimizeOrgImport throws InvalidOperationException


Are there some pitfalls when I import organization with OptimizeOrgImport=1? How this optimization works internally?

Use case:

  1. Import organization, say "test". (works fine)
  2. Import another copy of this organization, say "test2" (fails when updating users)
  3. Set OptimizeOrgImport=0 and import "test2" (fine again!)

Log of the step 2:

14:46:22|   Info| Import Organization (test2)
14:46:22|   Info| Update Organization...
14:48:20|   Info| Update Users...
14:48:44|  Error| Import Organization (Name=test2, Id=5b6bf441-650c-e211-b194-00185d111162) failed with Exception:
System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
   at System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.Crm.CrmDbConnection.InternalExecuteNonQuery(IDbCommand command)
   at Microsoft.Crm.CrmDbConnection.ExecuteNonQuery(IDbCommand command, Boolean impersonate)
   at Microsoft.Crm.CrmDbConnection.SetTransactionIsolationLevel(IsolationLevel il)
   at Microsoft.Crm.CrmDbConnection.Dispose(Boolean disposing)
   at Microsoft.Crm.SqlExecutionContext.Dispose(Boolean disposing)
   at Microsoft.Crm.SqlExecutionContext.Dispose()
   at Microsoft.Crm.Tools.Admin.ImportOrganizationInstaller.Import(Guid organizationId, String organizationUniqueName, String organizationFriendlyName, String sqlServerName, String databaseName, Uri reportServerUrl, String privilegedUserGroupName, String sqlAccessGroupName, String userGroupName, String reportingGroupName, String privilegedReportingGroupName, ICollection`1 users, MultipleTenancy multipleTenancy)

Thank you in adavance.


Solution

  • With the OptimizeOrgImport setting set to 1 you have to take following into account:

    When you use the registry entry OptimizeOrgImport and have the value of this entry set to 1, you cannot import the same organization database more than one time. If you want to import the same organization database more than one time, you must do one of the following things:

    • Set OptimizeOrgImport = 0 and use the normal import process.
    • Delete the organization, and then import the organization again.

    See http://support.microsoft.com/kb/977867/en

    Each organization database contains a table [dbo].[OrganizationBase]. One of its columns is OrganizationId. This GUID will identify the organization.

    If you make a backup of the Organization database on the CRM Environment Prod and restore it on the CRM Environment Test, it will keep its ID. If you later restore test2 it has the same ID as the one you have imported earlier.

    OptimizeOrgImport was introduced with Rollup 8 and introduced a new import process. I assume that the old import process (OptimizeOrgImport = 0) will also change the ID of the organization. Therefore you won't have the issue with the old import process.