Search code examples
c#ado.nettransactionscope

Is there some way to open a new connection that does not take part in the current TransactionScope?


My application needs to write to a table that acts as a system-wide counter, and this counter needs to be unique thorough the whole system and all his applications.

This counter is currently implemented as a table in our Oracle database.

My problem is: i need to write to this counter table (which use some keys to guarantee uniqueness of the counters at each business process into the system) without getting it locked in the current transaction, as multiple other processes may read or write into this table as well. Gaps in the counter does not matter, but i cannot create the counter values in advance.

Simple SEQUENCEs do not help me in this case, as the sequence number is a formatted number.

For now, i have some other non-db alternatives for achieving this, but i want to avoid changing the system code as much as i can for a while.

Actually, it would be simplest if i could open a new connection that won't take part in the current transaction, write to the table, and then close this new connection, but my tired mind can't find a way to do it.

The answer must be obvious, but the light just don't shine on me right now.


Solution

  • Execute your command inside a TransactionScope block created with the option "Suppress". This way your command won't participate in the current transaction.

    using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
    {
        // Execute your command here
    }
    

    For more information, see http://msdn.microsoft.com/en-us/library/system.transactions.transactionscopeoption.aspx