Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX -
Contents Index PreviousNext

Part 1 The Basic for Developing Application> Chapter 5 Transaction Function

5.3 Using Transactions with .NET APIs

This section provides an example of creating an application that uses .NET APIs.

The following example handles the deletion and updating of an XML document as a single transaction.

[Figure 5-2 Example of Handling the Deletion and Updating of an XML Document as a Single Transaction]

mark1Sample Code for C# .NET

/*
 * This program assumes that the following variables have been declared
 *  sQuery :Character string containing search expression
 *  sReturn :Character string containing return expression
 *  updateData :Character string containing update data
 */

ShunService service = null; 

try {
  // Create ShunService
  service = new ShunService();
  service.Host = "DirSvr1";
  service.Port = 33101; 
  service.Connect();

  // Create ShunSearchRequirement
  ShunSearchRequirement req = new ShunSearchRequirement();
  req.QueryExpression  = sQuery; 
  req.ReturnExpression = sReturn; 
  req.ReplyNumber      = 1;
  req.RequestCount     = 30; 

  // Execute search
  ShunResultSet rs = service.Search( req ); 

  // Turn off auto commit
  service.AutoCommit = false;                                            (1)

  try {
    // Target the third item of data for deletion
    ShunRecordCollection deleteRecCol = new ShunRecordCollection();
    deleteRecCol.Add( rs.Records[2] ); 
    
    // Delete data
    service.Delete( deleteRecCol ); 
    
    // Target the fifth item of data to be updated
    ShunRecordCollection updateRecCol = new ShunRecordCollection();
    ShunRecord record = rs.Records[4]; 
    record.Data = updateData; 
    updateRecCol.Add( record ); 
    
    // Update data
    service.Update( updateRecCol ); 
    
    // Execute commit
    service.Commit();                                                    (2)
  }
  catch {
    if ( service.State == ShunConnectionState.Open ) {
      // Execute rollback
      service.Rollback();                                               (3)
    }
    throw;
  }
  service.Disconnect();
}
catch ( ShunContinuousException e ) {
  // Processing to perform if a ShunContinuousException occurs
  try {
    if( service != null && service.State == ShunConnectionState.Open ) {
      service.Disconnect();
    }
  }
  catch( ShunException ex ) {
    Console.WriteLine("Error message : {0}", ex.Message );
  }
  Console.WriteLine("Error message : {0}", e.Message );
}
catch ( ShunTransactionRolledbackException e ) {
  // Processing to perform if a ShunTransactionRolledbackException occurs
  try {
    if( service != null && service.State == ShunConnectionState.Open ) {
      service.Disconnect();
    }
  }
  catch( ShunException ex ) {
    Console.WriteLine("Error message : {0}", ex.Message );
  }
  Console.WriteLine("Error message : {0}", e.Message );
}
catch ( ShunConnectionTerminatedException e ) {
  // Processing to perform if a ShunConnectionTerminatedException occurs
  Console.WriteLine("Error message : {0}", e.Message );
}

mark1Sample Code for VB .NET

' This program assumes that the following variables have been declared
' sQuery :Character string containing search expression
' sReturn :Character string containing return expression
' updateData :Character string containing update data

Dim service As ShunService = Nothing

Try
  ' Create ShunService
  service = New ShunService()
  service.Host = "DirSvr1"
  service.Port = 33101
  service.Connect()

  ' Create ShunSearchRequirement
  Dim req As ShunSearchRequirement = New ShunSearchRequirement()
  req.QueryExpression  = sQuery
  req.ReturnExpression = sReturn
  req.ReplyNumber      = 1
  req.RequestCount     = 30

  ' Execute search
  Dim rs As ShunResultSet = service.Search( req )

  ' Turn off auto commit
  service.AutoCommit = False                                            (1)

  Try
    ' Target the third item of data for deletion
    Dim deleteRecCol As ShunRecordCollection = New ShunRecordCollection()
    deleteRecCol.Add( rs.Records(2) )

    ' Delete data
    service.Delete( deleteRecCol )

    ' Target the fifth item of data to be updated
    Dim updateRecCol As ShunRecordCollection = New ShunRecordCollection()
    Dim record As ShunRecord = rs.Records(4)
    record.Data = updateData
    updateRecCol.Add( record )

    ' Update data
    service.Update( updateRecCol )

    ' Execute commit
    service.Commit()                                                    (2)

    Catch
      If service.State = ShunConnectionState.Open
    ' Execute rollback
        service.Rollback()                                              (3)
      End If
      Throw
    End Try
  
  service.Disconnect()

Catch e As ShunContinuousException
  ' Processing to perform if a ShunContinuousException occurs
  Try
    If Not service Is Nothing And service.State = ShunConnectionState.Open Then
      service.Disconnect()
    End If
  Catch ex As ShunException
    Console.WriteLine("Error message : {0}", ex.Message )
  End Try
  Console.WriteLine("Error message : {0}", e.Message )

Catch e As ShunTransactionRolledbackException
  ' Processing to perform if a ShunTransactionRolledbackException occurs
  Try
    If Not service Is Nothing And service.State = ShunConnectionState.Open Then
      service.Disconnect()
    End If
  Catch ex As ShunException
    Console.WriteLine("Error message : {0}", ex.Message )
  End Try
  Console.WriteLine("Error message : {0}", e.Message )

Catch e As ShunConnectionTerminatedException
  ' Processing to perform if a ShunConnectionTerminatedException occurs
  Console.WriteLine("Error message : {0}", e.Message )

End Try

mark1(1) Switching auto commit to disabled state

The AutoCommit property of the ShunService class is used to disable auto commit.

Switching is necessary because auto commit is enabled when a ShunService object is created.
Set the AutoCommit property to 'true' to enable auto commit again.

mark1(2) Executing commit operation

The Commit method is executed to terminate a transaction and update Shunsaku with the operations that have been performed.

mark1(3) Executing rollback

If an error occurs, the Rollback method is executed to roll back a transaction without updating Shunsaku with the operations that have been performed.

Refer to the .NET API Reference for more information on .NET APIs.

Down5.3.1 Error Handling

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006