Click or drag to resize
Using TransactionScope

Since version 2.1 SphinxConnector.NET supports the TransactionScope class. The TransactionScope class gives developers a comfortable way to use transactional resources. You just wrap the access to the transactional resources in a using block and the resource managers take care of the rest.

Using the TransactionScope Class with SphinxConnector.NET

Suppose that when you insert a new row in your database, you also want to add that data (or parts of it) to your Sphinx real-time index. But, in case the insert to Sphinx fails, you want the changes made to your database to be rolled back. This can be easily achieved with the TransactionScope class, which the following example demonstrates.

C#
using (TransactionScope ts = new TransactionScope())
{
    /*
     * Insert data into database here
     */

    using (SphinxQLConnection sphinxQLConnection = new SphinxQLConnection(connectionString))
    {
        SphinxQLCommand sphinxQLCommand = sphinxQLConnection.CreateCommand(@"INSERT into rt_index (id, title, content) 
                                                                             VALUES (@rtid, @title, @content)");

        sphinxQLCommand.Parameters.Add("rtid", 1, SphinxType.UInt);
        sphinxQLCommand.Parameters.Add("title", "My Title", SphinxType.String);
        sphinxQLCommand.Parameters.Add("content", "My Content", SphinxType.String);

        sphinxQLConnection.Open();

        sphinxQLCommand.ExecuteNonQuery();
    }

    ts.Complete();
}