久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么這個程序會生成兩封電子郵件?

Why is this procedure generating two Emails?(為什么這個程序會生成兩封電子郵件?)
本文介紹了為什么這個程序會生成兩封電子郵件?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

以下存儲過程由我們的代理每 5 分鐘運行一次 - 它使用此過程掃描表 Control_EmailQueue 以查看是否有任何新電子郵件要發送.

The following stored procedure is run by our agent every 5 mins - it scans the table Control_EmailQueue by using this proc to see if there are any new e-mails to send out.

我想測試當在表 Control_EmailQueue 中輸入不正確的電子郵件信息時 proc 的行為.兩個測試及其結果詳述如下.

I wanted to test how the proc behaves when incorrect email information is entered into the table Control_EmailQueue. Two tests and their results are detailed below.

TEST1
我向 Control_EmailQueue 添加了一條記錄,該記錄在所有 3 個字段 EmailTO/EmailCC 和 EmailBCC 中都有 NULL 條目.這工作正常,即錯誤被捕獲并且 CATCH 中的代碼被執行,所以我收到一封電子郵件,標題為 'ERROR OCCURED DURING EMAIL CREATION'

TEST1
I add a record to Control_EmailQueue which has NULL entries in all 3 fields EmailTO/EmailCC and EmailBCC. This works fine i.e and error is trapped and the code within CATCH is executed so I receive an email titled 'ERROR OCCURED DURING EMAIL CREATION'

TEST2
我將記錄添加到 Control_EmailQueue.在字段 EmailTO 中,我輸入這個字符串 'me@me.co.uk;xxxxxxx@xxxxx' 即第一個電子郵件地址有效但第二個電子郵件地址無效.當代理運行該程序時,me@me.co.uk 會收到一封電子郵件,但半秒后,me@me.co.uk<會收到另一封相同的電子郵件./代碼>.CATCH 代碼未在此測試中執行,因為未收到標題為 'ERROR OCCURED DURING EMAIL CREATION' 的電子郵件.

TEST2
I add a record to Control_EmailQueue. In the field EmailTO I enter this string 'me@me.co.uk; xxxxxxx@xxxxx' i.e. the first email address is valid but the second email address is not valid. When the procedure is run by the agent an email is received by me@me.co.uk but then half a second later another identical email is received by me@me.co.uk. The CATCH code is not executed in this test as the email titled 'ERROR OCCURED DURING EMAIL CREATION' is not received.

BEGIN TRY

    DECLARE @Exit TINYINT = 0
    WHILE @Exit = 0
        BEGIN

        BEGIN TRANSACTION

            DECLARE @MailIdFound INT =
            (
            SELECT 
                    CASE 
                            WHEN MIN([EmailId]) IS NULL THEN 0
                            ELSE MIN([EmailId])
                    END
            FROM [xxx].[console].[Control_EmailQueue]
            WHERE
                    [DateInsertKey] IS NOT NULL 
                    AND
                        ( --the following gives option to re-run past mails by updating DateEmailKey to NULL
                        [DateEmailKey] IS NULL
                        OR
                        [DateEmailKey] < [DateInsertKey]
                        )
                    AND 
                    ErrorOccured = 0
                    AND 
                    EmailActive = 1
            )

            IF @MailIdFound = 0 
            BEGIN SET @Exit = 1 END --exit here as  
            ELSE

            BEGIN --send the mail here

                    --DECLARE @EmailId INT
                    DECLARE @DateInsertKey INT
                    DECLARE @DateEmailKey INT
                    DECLARE @CallingReportName NVARCHAR(1000)
                    DECLARE @EmailBCC  NVARCHAR(1000)
                    DECLARE @EmailTO  NVARCHAR(1000)
                    DECLARE @EmailCC NVARCHAR(1000)
                    DECLARE @EmailBody NVARCHAR(MAX)
                    DECLARE @EmailAttachmentPath NVARCHAR(1000)
                    DECLARE @EmailImportance VARCHAR(6)
                    DECLARE @EmailSubject NVARCHAR(1000)

                    ;WITH myMostUrgentMail_cte
                    AS
                            (
                            SELECT 
                                    TOP 1
                                    --[EmailId],
                                    [DateInsertKey],
                                    [DateEmailKey],
                                    [CallingReportName],
                                    [EmailBCC],
                                    [EmailTO],
                                    [EmailCC],
                                    [EmailBody],
                                    [EmailAttachmentPath],
                                    [EmailImportance],
                                    [EmailSubject]
                            FROM [xxx].[console].[Control_EmailQueue]
                            WHERE [EmailId] = @MailIdFound
                            )
                    SELECT 
                            @DateInsertKey          = [DateInsertKey],
                            @DateEmailKey           = [DateEmailKey],
                            @CallingReportName = [CallingReportName],
                            @EmailTO                    = [EmailTO],        
                            @EmailCC                    = [EmailCC],                        
                            @EmailBCC               = [EmailBCC],
                            @EmailBody              = [EmailBody],
                            @EmailAttachmentPath = [EmailAttachmentPath],
                            @EmailImportance        = CASE 
                                                                                WHEN [EmailImportance] = 0 THEN 'Low'
                                                                                WHEN [EmailImportance] = 1 THEN 'Normal'
                                                                                WHEN [EmailImportance] = 2 THEN 'High'
                                                                    END,
                            @EmailSubject           = [EmailSubject]
                    FROM myMostUrgentMail_cte


                    SET @EmailBody = @EmailBody + '<b>Please contact us with any questions</b></p></span></html>'
                    EXEC msdb..sp_send_dbmail
                            @recipients                     = @EmailTO,  
                            @copy_recipients            = @EmailCC,
                            @blind_copy_recipients  = @EmailBCC,
                            @subject                            = @EmailSubject,
                            @file_attachments          = @EmailAttachmentPath,
                            @Importance                 = @EmailImportance,
                            @body_format                    = 'html',
                            @body                               = @EmailBody    

                    UPDATE x
                    SET 
                                x.[DateEmailKey]        = (CONVERT(CHAR(8),GETDATE(),(112))),
                                x.[DateEmailTime]   = (CONVERT([time](7),left(CONVERT([char](12),GETDATE(),(114)),(8)),(0)))
                    FROM [xxx].[console].[Control_EmailQueue] x
                    WHERE x.[EmailId] = @MailIdFound

            END

        COMMIT TRANSACTION

        END

END TRY



BEGIN CATCH

     IF @@trancount>0 
        BEGIN
                ROLLBACK TRANSACTION
        END

    -- handle error here
    DECLARE @ErrorMessage VARCHAR(100) =  '<html><p>Error occured during creation of EmailId: ' + CONVERT(VARCHAR(10),@MailIdFound) + '</p><p>xxx.console.Control_EmailQueue</p></html>'
    EXEC msdb..sp_send_dbmail
            @recipients = 'me@me.co.uk;'
            , @subject = 'ERROR OCCURED DURING EMAIL CREATION'
            , @body_format = 'html'
            , @body = @ErrorMessage

    UPDATE x
    SET x.ErrorOccured = 1
    FROM [xxx].[console].[Control_EmailQueue] x
    WHERE x.[EmailId] = @MailIdFound

END CATCH;
END

推薦答案

該問題似乎與事務的計時有關.通過在提交后添加延遲,事務能夠在執行下一個循環之前完成并提交.

The problem appears to be related to timing with the transaction. By adding a delay after the commit, the transaction is able to complete and commit prior to the next loop being executed.

您可能應該做的一件事是從 sp_send_dbmail 獲取 mailitem_id.也許你是對的,它失敗了,但沒有出錯,但這不應該影響交易.我唯一能想到的是,由于事務尚未實際提交,因此您正在執行臟讀或幻讀,因此小延遲允許實際提交數據.

One thing you should probably do is get the mailitem_id from sp_send_dbmail. Perhaps you are correct and it is failing, but not erroring, but that shouldn't impact the transaction. The only thing I can think is that you are getting dirty or phantom reads because the transaction isn't actually committed yet, so the small delay allows the data to actually be committed.

這篇關于為什么這個程序會生成兩封電子郵件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产精品污www一区二区三区 | 国产精品成人一区二区三区 | 免费亚洲成人 | 日韩电影中文字幕 | 国产一级片久久久 | 国产精品久久久乱弄 | 天堂资源| 国产精品色哟哟网站 | 国产在线精品免费 | 97精品超碰一区二区三区 | 最新91在线 | 亚洲成人一区 | 亚洲精品黄色 | 欧美一页 | 男女羞羞免费视频 | 不卡视频在线 | 免费视频一区二区 | 亚洲一区二区三区在线 | 亚洲精品在线免费 | 欧美一区中文字幕 | 国产乱码精品一区二区三区忘忧草 | 亚洲国产成人在线 | 成人免费在线观看 | a级网站 | 91久久| 国产精品区一区二区三区 | 国产成人精品综合 | 澳门永久av免费网站 | 天天综合天天 | 日韩欧美大片在线观看 | 国产日韩精品视频 | 日韩欧美在线一区 | 日本高清中文字幕 | 特级特黄特色的免费大片 | 国产午夜精品久久久 | 毛片视频免费观看 | 搞av.com| 欧美精品一区二区三区在线播放 | 日韩毛片免费看 | 国产成人在线视频免费观看 | 四虎国产 |