sharepoint之transaction

http://weblogs.asp.net/paolopia/archive/2005/01/30/363312.aspx

Handling transactions working on Windows Sharepoint Services lists

One of the features I miss on WSS and SPS is the capability to change lists contents and configuration transactionally.When you work on complex scenarios often it should be usefull to change data on an "application/service/support database" and on WSS/SPS lists/contents, but the Object Model of Sharepoint doesn't give us a way to handle transactions. So you have to do it manually, with compensating transactions.

By the way WSS and SPS 2003 are based on Windows Server 2003, we know. With Windows Server 2003 we also have COM+ 1.5 that allows us to build Services Without Components. There're many blogs and articles out there, about SWC and Serviced Components, so if you don't know what they are ... just google it.

Here is a code snippet that creates a WSS list covered by a transaction, provided transparently by a Service Without Components:

using System;
using System.EnterpriseServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace WSSTransactional
{
 class ShowWSST
 {
  [STAThread]
  static void Main(string[] args)
  {
   // Prepare the SWC context in COM+ 1.5
   ServiceConfig sc = new ServiceConfig();
   sc.TrackingEnabled = true;
   sc.TrackingAppName = "Demo";
   sc.TrackingComponentName = "WSSTX Demo";
   sc.Binding = BindingOption.BindingToPoolThread;
   sc.Transaction = TransactionOption.RequiresNew;
   sc.IsolationLevel = TransactionIsolationLevel.ReadCommitted;

   // Enter the context
   ServiceDomain.Enter(sc);

   // Check if the transaction is available
   if (!ContextUtil.IsInTransaction)
    throw new Exception("Transaction failed!");

   try
   {
    SPSite site = new SPSite("http://wssdev2k3:8001/");
    SPWeb web = site.OpenWeb();

    Guid folderId = web.Lists.Add("My Agenda", String.Empty, SPListTemplateType.Events);
    SPList folderList = web.Lists[folderId];
    folderList.OnQuickLaunch = true;
    folderList.Title = "My Agenda";

    // Save the list configuration in WSS
    folderList.Update();
   }
   catch (Exception ex)
   {
    // In case of any error, rollback => SetAbort
    if (ContextUtil.IsInTransaction) ContextUtil.SetAbort();
    throw ex;
   }
  
   Console.WriteLine("Would you like to commit the operation (Y/N) ?");
   String result = Console.ReadLine();
   if (result.ToUpper().Trim() == "Y")
    ContextUtil.EnableCommit();
   else
    ContextUtil.SetAbort();
   
   TransactionStatus status = TransactionStatus.NoTransaction;
   if (ContextUtil.IsInTransaction)
    status = ServiceDomain.Leave();

   if ((status != TransactionStatus.Commited) && (status != TransactionStatus.NoTransaction) && (status != TransactionStatus.LocallyOk))
    throw new Exception("Operation failed! Transaction aborted!");
  }
 }
}

As you can see the code that works with WSS/SPS doesn't care of the SWC code. I think this is very usefull! Hope you agree :-) !

你可能感兴趣的:(transaction)