示例#1
0
        protected override bool ShouldRetryOn(Exception exception)
        {
            if (_additionalErrorNumbers != null)
            {
                if (SqlServerTransientExceptionDetector.IsHandled(exception, out var errorNumbers))
                {
                    foreach (var errNumber in errorNumbers)
                    {
                        if (_additionalErrorNumbers.Contains(errNumber))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(SqlServerTransientExceptionDetector.ShouldRetryOn(exception));
        }
示例#2
0
        static bool IsMemoryOptimizedError(Exception exception)
        {
            if (SqlServerTransientExceptionDetector.IsHandled(exception, out var errorNumbers))
            {
                foreach (var errNumber in errorNumbers)
                {
                    switch (errNumber)
                    {
                    case 41301:
                    case 41302:
                    case 41305:
                    case 41325:
                    case 41839:
                        return(true);
                    }
                }
            }

            return(false);
        }
        protected override bool ShouldRetryOn(Exception exception)
        {
            if (_additionalErrorNumbers != null)
            {
                var sqlException = exception as SqlException;

                if (sqlException != null)
                {
                    foreach (SqlError err in sqlException.Errors)
                    {
                        if (_additionalErrorNumbers.Contains(err.Number))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(SqlServerTransientExceptionDetector.ShouldRetryOn(exception));
        }
        private static SqlServerProviderAdapter CreateAdapter(string assemblyName, string clientNamespace, string factoryName)
        {
            var isSystem = assemblyName == SystemAssemblyName;

            Assembly?assembly;

#if NET45 || NET46
            if (isSystem)
            {
                assembly = typeof(System.Data.SqlClient.SqlConnection).Assembly;
            }
            else
#endif
            {
                assembly = Common.Tools.TryLoadAssembly(assemblyName, factoryName);
            }

            if (assembly == null)
            {
                throw new InvalidOperationException($"Cannot load assembly {assemblyName}");
            }

            var connectionType                 = assembly.GetType($"{clientNamespace}.SqlConnection", true);
            var parameterType                  = assembly.GetType($"{clientNamespace}.SqlParameter", true);
            var dataReaderType                 = assembly.GetType($"{clientNamespace}.SqlDataReader", true);
            var transactionType                = assembly.GetType($"{clientNamespace}.SqlTransaction", true);
            var commandType                    = assembly.GetType($"{clientNamespace}.SqlCommand", true);
            var sqlCommandBuilderType          = assembly.GetType($"{clientNamespace}.SqlCommandBuilder", true);
            var sqlConnectionStringBuilderType = assembly.GetType($"{clientNamespace}.SqlConnectionStringBuilder", true);
            var sqlExceptionType               = assembly.GetType($"{clientNamespace}.SqlException", true);
            var sqlErrorCollectionType         = assembly.GetType($"{clientNamespace}.SqlErrorCollection", true);
            var sqlErrorType                   = assembly.GetType($"{clientNamespace}.SqlError", true);

            var sqlDataRecordType = connectionType.Assembly.GetType(
                isSystem
                                        ? "Microsoft.SqlServer.Server.SqlDataRecord"
                                        : "Microsoft.Data.SqlClient.Server.SqlDataRecord",
                true);

            var bulkCopyType                        = assembly.GetType($"{clientNamespace}.SqlBulkCopy", true);
            var bulkCopyOptionsType                 = assembly.GetType($"{clientNamespace}.SqlBulkCopyOptions", true);
            var bulkRowsCopiedEventHandlerType      = assembly.GetType($"{clientNamespace}.SqlRowsCopiedEventHandler", true);
            var bulkCopyColumnMappingType           = assembly.GetType($"{clientNamespace}.SqlBulkCopyColumnMapping", true);
            var bulkCopyColumnMappingCollectionType = assembly.GetType($"{clientNamespace}.SqlBulkCopyColumnMappingCollection", true);
            var rowsCopiedEventArgsType             = assembly.GetType($"{clientNamespace}.SqlRowsCopiedEventArgs", true);

            var typeMapper = new TypeMapper();

            typeMapper.RegisterTypeWrapper <SqlConnection>(connectionType);
            typeMapper.RegisterTypeWrapper <SqlParameter>(parameterType);
            typeMapper.RegisterTypeWrapper <SqlTransaction>(transactionType);
            typeMapper.RegisterTypeWrapper <SqlErrorCollection>(sqlErrorCollectionType);
            typeMapper.RegisterTypeWrapper <SqlException>(sqlExceptionType);
            typeMapper.RegisterTypeWrapper <SqlError>(sqlErrorType);
            typeMapper.RegisterTypeWrapper <SqlConnectionStringBuilder>(sqlConnectionStringBuilderType);

            // bulk copy types
            typeMapper.RegisterTypeWrapper <SqlBulkCopy>(bulkCopyType);
            typeMapper.RegisterTypeWrapper <SqlBulkCopyOptions>(bulkCopyOptionsType);
            typeMapper.RegisterTypeWrapper <SqlRowsCopiedEventHandler>(bulkRowsCopiedEventHandlerType);
            typeMapper.RegisterTypeWrapper <SqlBulkCopyColumnMapping>(bulkCopyColumnMappingType);
            typeMapper.RegisterTypeWrapper <SqlBulkCopyColumnMappingCollection>(bulkCopyColumnMappingCollectionType);
            typeMapper.RegisterTypeWrapper <SqlRowsCopiedEventArgs>(rowsCopiedEventArgsType);
            typeMapper.FinalizeMappings();

            var paramMapper        = typeMapper.Type <SqlParameter>();
            var dbTypeBuilder      = paramMapper.Member(p => p.SqlDbType);
            var udtTypeNameBuilder = paramMapper.Member(p => p.UdtTypeName);
            var typeNameBuilder    = paramMapper.Member(p => p.TypeName);

            SqlServerTransientExceptionDetector.RegisterExceptionType(sqlExceptionType, exceptionErrorsGettter);

            return(new SqlServerProviderAdapter(
                       connectionType,
                       dataReaderType,
                       parameterType,
                       commandType,
                       transactionType,
                       sqlDataRecordType,
                       sqlExceptionType,

                       dbTypeBuilder.BuildSetter <IDbDataParameter>(),
                       dbTypeBuilder.BuildGetter <IDbDataParameter>(),
                       udtTypeNameBuilder.BuildSetter <IDbDataParameter>(),
                       udtTypeNameBuilder.BuildGetter <IDbDataParameter>(),
                       typeNameBuilder.BuildSetter <IDbDataParameter>(),
                       typeNameBuilder.BuildGetter <IDbDataParameter>(),

                       typeMapper.BuildWrappedFactory((string connectionString) => new SqlConnectionStringBuilder(connectionString)),
                       typeMapper.BuildWrappedFactory((string connectionString) => new SqlConnection(connectionString)),

                       typeMapper.BuildWrappedFactory((IDbConnection connection, SqlBulkCopyOptions options, IDbTransaction? transaction) => new SqlBulkCopy((SqlConnection)connection, options, (SqlTransaction?)transaction)),
                       typeMapper.BuildWrappedFactory((int source, string destination) => new SqlBulkCopyColumnMapping(source, destination))));

            IEnumerable <int> exceptionErrorsGettter(Exception ex) => typeMapper.Wrap <SqlException>(ex).Errors.Errors.Select(err => err.Number);
        }