Basic Usage Pattern

The basic pattern for using the transactional API is simple:

  1. Create an instance of the NetworkedPlanet.TMCore.Persistence.TransactionalPersistenceManager class.

    The constructor takes an ITopicMapSystem instance as a parameter. So if you have an ITopicMap instance already, then you can use code like this:

    // in the using section...
    using NetworkedPlanet.TMCore.Persistence;
    	  
    // in your code:
    TransactionalPersistenceManager tmpm = 
        new TransactionalPersistenceManager(tm.TopicMapSystem);
    	
  2. Use the TransactionalPersistenceManager to create a new NetworkedPlanet.TMCore.Transaction class instance. This is achieved by calling the StartTransaction method:

    Transaction txn = tmpm.StartTransaction();
    	
  3. Make operation calls by invoking the methods of the Transaction class. See the API HTML Help file for a detailed description of each method. The sample code below creates a new topic and assigns a name and subject identifier to it. Note that the return values of the Transaction methods can be used in subsequent method calls in the same transaction, so it is possible to create a new topic and add names, occurrences and connect it into associations it all in a single transaction.

      int personId = txn.AddTopic(tm.ID, personType.ID); // assume personType is an ITopic instance
      txn.AddTopicName(person, -1, "John Smith", null, null);
      txn.AddSubjectIdentifier(person, -1, "http://www.example.com/people/jsmith");
    	
  4. Commit all changes by calling the Commit() method of the Transaction instance or rollback changes by calling the Rollback() method.

    // Sample structure for using a transaction
    Transaction txn = tmpm.StartTransaction();
    try 
    {
      // Call Transaction methods here
      txn.Commit();
    } 
    catch (Exception ex) 
    {
      txn.Rollback();
      // Handle exception or rethrow
    }