Can I reuse a transaction? And How?

I have to use an enterprise class that reuses or re-creates a transaction after each Commit()

or Rollback()

. This class is encouraged to use (or not use) transactions using the Boolean

ctor parameter .

I'm going to wrap this API to separate transaction support (to explicitly refer to Transaction or ambient objects TransactionScope

). But this requires a transaction class that can be reused. Is there such a class in .NET? Or how will I start developing my own?

+1


source to share


3 answers


Update: The type Session

has a separate behavior that does not support a commit transaction. I can personally confirm the messages I have completed. This allows me to avoid a session based transaction.



0


source


No, transactions cannot be reused after they have been committed or rolled back (and I believe that trying to access them after they have been committed or rolled back will throw an exception).



My advice would be to forget about trying to create a wrapper for the TransactionScope, as it doesn't really make any difference since the TransactionScope model is implicit to start with (and it's its own wrapper). Just throw your expressions using (TransactionScope ts = new TransactionScope())

around the things that need to be done. With TransactionScope, every call to the database implies support for transactions.

+3


source


Write a wrapper way this way

TransactionScope GetTransaction(bool useTransaction)
{
   if (useTransaction)
   {
      return new TransactionScope( /* ... */ );
   }
   return null;
}

      

Using blocks will work with null references, so you could write your code like this (where "useTransaction" is the boolean parameter you give your class):

using (var scope = GetTransaction(useTransaction))
{
   // Code here (if useTransaction == true will use the ambient Transaction,
   // otherwhise will not run inside a transaction).

   if (scope != null)
   {
      scope.Complete();
   }
}

      

0


source







All Articles