WF中的跟踪服务(5):SqlTrackingService 的数据维护

简介:

相关文章:
WF中的跟踪服务(4):使用跟踪配置文件
WF中的跟踪服务(3):使用SqlTrackingService跟踪规则
WF中的跟踪服务(2):使用SqlTrackingService
WF中的跟踪服务(1):Sql跟踪数据库表,视图,存储过程等相关说明

WF框架中内置的SqlTrackingService服务提供了数据维护的功能,可以将跟踪数据库中的信息移动到指定的分区中。利用该功能我们可以有效的管理这些不断增长的跟踪数据,使得维护和数据的分析更容易。

分区方案

WF 提供两种分区的方案,自动和手动两种方式。默认情况下SqlTrackingService 的分区功能是没有启用的,要想启用需要将SqlTrackingService 的PartitionOnCompletion 属性设置为 true,如果启用了分区,则会为跟踪数据定期创建一组新的表,以便能够将旧数据存档并将其从主数据库中删除,而不会影响当前运行的应用程序。 在启用分区的情况下,将仍然在原始表中创建跟踪数据,但随后当工作流完成时,这些数据将被移到分区表中。

1.自动的方式

该方式不会影响应用程序的正常运行。 分区是在每个工作流实例完成时执行的。 由于跟踪工作流实例而生成的记录会添加到常规跟踪表中,直到工作流实例完成。 届时,这些记录会移至分区表中。 当启用了分区时,会定期为跟踪数据创建一组新的表。 已完成的工作流实例的跟踪数据会移至这些新的分区中,同时不会中断当前正在运行的应用程序。默认的间隔是每月的,如果你要改变你就使用SetPartitionInterval存储过程。该存储过程接收一个单独的参数表示新的间隔,d(daily),w(weekly),m(monthly),and y(yearly)这几个值。该值存储在TrackingPartitionInterval表中。表 vw_TrackingPartitionSetName 包含有关分区集名称的信息,包括 Name 、CreatedDateTime、EndDateTime 以及 PartitionInterval。

2.手动的方式

手动的方式是在应用程序停止的时候进行的,使用PartitionCompletedWorkflowInstances 存储过程将工作流实例从实时表中插入的跟踪数据移到分区表中。这两种方式各有优缺点。 自动的会影响性能,特别是数据量很大的时候。 如果你不想有性能的损失,就手动做,默认情况PartitonOnCompletion属性就是false的,一般在你停机时候来做分区。


移除分区

当你不再需要这个分区的时候,你有两种选择,一种是分离,一种是删除。使用DetachPartiton存储过程,就逻辑上从标准的视图中移除了分区。实际上内部执行的是RebulidPartitonViews存储过程来更新视图。就是说当你分离这些分区的时候,这些表并没有删除。使用DropPartiton存储过程物理上删除表。这两个存储过程都接收一个分区名称和分区id,你可以通过查询vw_TrackingPartitonSetName来得到这些值。

实例分析

1.我们将相关的操作封装一个类SqlDataMaintenanceHelper,代码如下:

public class SqlDataMaintenanceHelper
{
   public const string strConn = "Initial Catalog=WorkflowTracking;Data Source=localhost;
        Integrated Security=SSPI;";
   //设置分区时间周期
    public static void SetPartitionInterval(char interval)
   {
      try
      {
        using (SqlConnection conn = new SqlConnection(strConn))
        {
           SqlCommand command = new SqlCommand("SetPartitionInterval", conn);
           command.CommandType = CommandType.StoredProcedure;
           command.Parameters.Add( new SqlParameter("@Interval", interval));
           command.Connection.Open();
           command.ExecuteNonQuery();
        }
      }
      catch (SqlException e)
      {
           Console.WriteLine("SqlException in SetPartitionInterval: {0}", e.Message);
      }
   }
   //删除分区
    public static void DropPartition(Int32 partitionId)
   {
     try
     {
        using (SqlConnection conn = new SqlConnection(strConn))
        {
           SqlCommand command= new SqlCommand("DropPartition", conn);
           command.CommandType = CommandType.StoredProcedure;
           command.Parameters.Add(new SqlParameter("@PartitionName", null));
           command.Parameters.Add(new SqlParameter("@PartitionId", partitionId));
           command.Connection.Open();
           command.ExecuteNonQuery();
         }
         Console.WriteLine("Partition {0} dropped", partitionId);
       }
       catch (SqlException e)
       {
           Console.WriteLine("SqlException in DropPartition: {0}", e.Message);
       }
    }
    public static void DetachPartition(Int32 partitionId)
    {
      try
      {
        using (SqlConnection conn = new SqlConnection(strConn))
        {
           SqlCommand command = new SqlCommand("DetachPartition", conn);
           command.CommandType = CommandType.StoredProcedure;
           command.Parameters.Add(new SqlParameter("@PartitionName", null));
           command.Parameters.Add(new SqlParameter("@PartitionId", partitionId));
           command.Connection.Open();
           command.ExecuteNonQuery();
         }
         Console.WriteLine("Partition {0} dropped", partitionId);
       }
       catch (SqlException e)
       {
           Console.WriteLine("SqlException in DropPartition: {0}", e.Message);
       }
     }
    //手动执行分区
     public static void PartitionCompletedInstances()
    {
      try
      {
        using (SqlConnection conn= new SqlConnection(strConn))
        {
           SqlCommand command = new SqlCommand("PartitionCompletedWorkflowInstances", conn);
           command.CommandType = CommandType.StoredProcedure;
           command.Connection.Open();
           command.ExecuteNonQuery();
        }
        Console.WriteLine("Partitioning of workflows completed");
       }
       catch (SqlException e)
       {
           Console.WriteLine("SqlException in PartitionCompletedInstances: {0}", e.Message);
       }
     }
    //显示分区信息
     public static void ShowAllPartitions()
    {
      try
      {
        using (SqlConnection conn= new SqlConnection(strConn))
        {
           SqlCommand command = new SqlCommand(@"SELECT * from vw_TrackingPartitionSetName
                     ORDER BY PartitionId", conn);
           command.Connection.Open();
           Console.WriteLine("--显示分区相关信息--");
           Console.WriteLine("{0,-4} {1,-13} {2,-22} {3,-20} {4,-4}",
                "Id", "Name", "CreatedDateTime", "EndDateTime", "PartitionInterval");
           using (SqlDataReader reader = command.ExecuteReader())
           {
               while (reader.Read())
               {
                  Console.WriteLine("{0,-4} {1,-13} {2,-22} {3,-20} {4,-4}",
                       reader["PartitionId"],
                       reader["Name"],
                       reader["CreatedDateTime"],
                       reader["EndDateTime"],
                       reader["PartitionInterval"]); 
                }
                reader.Close();
             }
             Console.WriteLine("--显示分区相关信息完成--");
             Console.WriteLine();
            }
          }
          catch (SqlException e)
          {
             Console.WriteLine("SqlException in DisplayAllPartitions: {0}", e.Message);
          }
    }
    //显示分区表信息
     public static void ShowPartitionTableInfo()
    {
      try
      {
        using (SqlConnection conn = new SqlConnection(strConn))
        {
           SqlCommand command = new SqlCommand("declare @trackingName varchar(255) select
            @trackingName = Name from TrackingPartitionSetName " +"select name from sysobjects
            where name like '%' + @trackingName", conn);
           command.Connection.Open();
           Console.WriteLine("--显示分区表相关信息--");
           using (SqlDataReader reader = command.ExecuteReader())
           {
              while (reader.Read())
              {
                 Console.WriteLine(reader[0]);
              }
              reader.Close();
           }
           Console.WriteLine("--显示分区表相关信息完成--");
           Console.WriteLine();
         }
        }
        catch (SqlException e)
        {
          Console.WriteLine("SqlException in ShowPartitionTableInformation: {0}", e.Message);
        }
      }
    }
2.我们建立一个最简单的工作流,只放一个CodeActivity,宿主程序中我们加载SqlTrackingService服务,
并使用上面的类来显示相关信息,代码如下:
class Program
{
   static void Main(string[] args)
   {
      using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
      {
         SqlTrackingService sts = new SqlTrackingService(SqlDataMaintenanceHelper.strConn);
         sts.PartitionOnCompletion = false;
         SqlDataMaintenanceHelper.SetPartitionInterval('d');
         workflowRuntime.AddService(sts);
                
         AutoResetEvent waitHandle = new AutoResetEvent(false);
         workflowRuntime.WorkflowCompleted += delegate(object sender, 
WorkflowCompletedEventArgs e){waitHandle.Set();}; workflowRuntime.WorkflowTerminated += delegate(object sender,
WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception.Message); waitHandle.Set(); }; WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof
(CaryDataMTDemo.Workflow1)); Console.WriteLine("--工作流开始执行--"); instance.Start(); waitHandle.WaitOne(); Console.WriteLine("--工作流执行结束--"); Console.WriteLine(); SqlDataMaintenanceHelper.ShowAllPartitions(); SqlDataMaintenanceHelper.ShowPartitionTableInfo(); } } }
3.程序执行的结果如下:
 
我们可以看看数据库中表和视图的变化,如下图:
 

4.如果你删除当前的分区时,会报下面的错误:

SqlException in DropPartition: DetachPartition 失败 - 分区正处于 Activity 状态或处于翻转期。

这是因为你不能使当前分区处于非活动状态,因为如果可以的话,你就没有办法在工作流实例完成的时候将跟踪数据转移到分区中了。你只能分离或删除非活动的分区。当分区的间隔过期后就会产生新的分区,这个时候新的分区就成为了活动分区,原来的就变为非活动分区,这个时候就可以删除了。


本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2008/11/30/SqlDataMaintenance.html,如需转载请自行联系原作者

相关文章
SAP PM 入门系列10 - 根据维护通知单创建维护订单
SAP PM 入门系列10 - 根据维护通知单创建维护订单
SAP PM 入门系列10 - 根据维护通知单创建维护订单
SAP PM 入门系列11 - 一个维护通知单只能创建一个维护订单?
SAP PM 入门系列11 - 一个维护通知单只能创建一个维护订单?
SAP PM 入门系列11 - 一个维护通知单只能创建一个维护订单?
|
搜索推荐
SAP QM QA08批量维护QMAT数据
SAP QM QA08批量维护QMAT数据
SAP QM QA08批量维护QMAT数据