Monday, November 20, 2017

ESBExceptionDB Maintance

ESBExceptionDB is a Database that is used to capture the Exceptions and used for Alert notifications, resubmitting of the messages ( with a bit of customization).

But over a period of time this Database will start to accumulate the messages. We don't have out-box jobs to clean the Database. We need to set up custom job similar to  dtasp_BackupAndPurgeTrackingDatabase to clean Faults. Below is the script that can be used to Maintain the DB. We can further Enhance ESB Portal to allow User to delete the messages after reviewing the Expection, but in any case we need a custom job/script to clean up the DataBase.

USE [EsbExceptionDb]
GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE [dbo].[usp_PurgeEsbExceptionDb]

    @DaysToKeep    INT = 30
AS
BEGIN
/*
=============================================
Example:
EXEC [dbo].[usp_PurgeEsbExceptionDb] @DaysToKeep = 30
=============================================
*/
    DECLARE @currentDateTime DATETIME, @deleteFromDateTime DATETIME;

    SET @currentDateTime = GETUTCDATE();

    SET @deleteFromDateTime = @currentDateTime - @DaysToKeep;
    SET NOCOUNT ON;

    /*Deleting all the records from ContextProperty table*/

    DELETE conprop FROM [dbo].[ContextProperty] conprop
    INNER JOIN
    (SELECT msg.MessageID FROM [dbo].[Message] (NOLOCK) msg
    INNER JOIN
    (SELECT fault.FaultID FROM [dbo].[Fault](NOLOCK) fault WHERE fault.InsertedDate < @deleteFromDateTime) faultsToDelete
    ON faultsToDelete.FaultID = msg.FaultID) msgIdsToDelete
    ON msgIdsToDelete.MessageID = conprop.MessageID

    /*Deleting all the records from ProcessedFault table*/

    DELETE procfaults FROM [dbo].[ProcessedFault](NOLOCK) procfaults
    INNER JOIN
    (SELECT fault.FaultID FROM [dbo].[Fault](NOLOCK) fault WHERE fault.InsertedDate < @deleteFromDateTime) faultsToDelete
    ON procfaults.ProcessedFaultID = faultsToDelete.FaultID

    /*Deleting all the records from MessageData table*/

    DELETE msgdata FROM [dbo].[MessageData] msgdata
    INNER JOIN
    (SELECT msg.MessageID FROM [dbo].[Message](NOLOCK) msg
    INNER JOIN
    (SELECT fault.FaultID FROM [dbo].[Fault](NOLOCK) fault WHERE fault.InsertedDate < @deleteFromDateTime) faultsToDelete
    ON faultsToDelete.FaultID = msg.FaultID) msgIdsToDelete
    ON msgIdsToDelete.MessageID = msgdata.MessageID

    /*Deleting all the records from Message table*/

    DELETE msg FROM [dbo].[Message] msg
    INNER JOIN
    (SELECT fault.FaultID FROM [dbo].[Fault](NOLOCK) fault WHERE fault.InsertedDate < @deleteFromDateTime) faultsToDelete
    ON msg.FaultID = faultsToDelete.FaultID

    /*Deleting all the records from Fault table*/

    DELETE fault FROM [dbo].[Fault](NOLOCK) fault WHERE fault.InsertedDate < @deleteFromDateTime;

    /*Deleting all the records from AlertSubscriptionHistory table*/

    DELETE ash FROM [dbo].[AlertSubscriptionHistory] (NOLOCK) ash WHERE ash.InsertedDate < @deleteFromDateTime;

    /*Deleting all the records from AlertHistory table*/

    DELETE ah FROM [dbo].[AlertHistory] (NOLOCK) ah WHERE ah.InsertedDate < @deleteFromDateTime;

    /*Deleting all the records from AlertEmail table*/

    DELETE ae FROM [dbo].[AlertEmail] (NOLOCK) ae WHERE ae.InsertedDate < @deleteFromDateTime;

    SET NOCOUNT OFF;

END

GO

No comments: