How to prevent NServiceBus from rolling back the transaction or parts of it?

Konrad Viltersten

We have a handler that performs as supposed to when it's received a message. There are several steps in the process and each consecutive one creates its own, new context, as shown below.

public void Handle(SomeMessage message)
{
  ...
  try
  {
    ...
    using(DataContext context = new DataContext()) { ... }
    ...
    using(DataContext context = new DataContext()) { ... }
    ...
  }
  catch (Exception exception)
  { 
    ...
    using(DataContext context = new DataContext()) { ... }
    ...
  }
}

However, on occasion, an exception is thrown and caught in it. The current transaction is rolled back (and since it's not implemented by me explicitly, I'm assuming, without being utterly certain, that it's done by the NServiceBus itself because all the results written to the DB disappear, including those in the catch statement).

At the moment, I'm resolving it by writing to a file when something terrible happens but I'd love to have that stored in the DB as well.

So, ultimately, I'd like to prevent that the last operation, the one in catch statement, is rolled back. Can one do that and if so how?

Dennis van der Stelt

I'd really like to know the bigger issue, what are you trying to achieve? Why do you want to log something to the database when an error occurs? If it's technical related, the messages should start appearing in the error queue, you should fix the error and have NServiceBus retry the messages. When it's business related, you should have an alternate route to handle the specific business requirement. For example you're out of stock of a specific item, then it might be better to email the buyer to wait for their item or tell them they can get their money back. And inform sales that someone bought an item out of stock, or something like it.

NServiceBus does rollback all transactions, but this is a TransactionScope related issue. Start a new TransactionScope and tell it to be a new root transaction.

using(TransactionScope scope1 = new TransactionScope()) 
//Default is Required 
{ 
     using(TransactionScope scope2 = new 
      TransactionScope(TransactionScopeOption.Required)) 
     {
     ...
     } 

     using(TransactionScope scope3 = new TransactionScope(TransactionScopeOption.RequiresNew)) 
     {
     ...
     } 

     using(TransactionScope scope4 = new 
        TransactionScope(TransactionScopeOption.Suppress)) 
    {
     ...
    } 
}

The scope3 transaction is a transaction on its own. If you want, put this inside your catch scope.

This article explains it all. But again, I'd rather have more information on what you're actually trying to achieve. And perhaps read up on this article.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to prevent NServiceBus from rolling back the transaction or parts of it?

From Dev

How to prevent a specific INSERT statement from rolling back in a transaction

From Dev

How to prevent a specific INSERT statement from rolling back in a transaction

From Dev

Prevent inner transaction from rolling back outer transaction

From Dev

Prevent inner transaction from rolling back outer transaction

From Dev

TSQL: Prevent trigger suppressing error but rolling back transaction

From Dev

Rails transaction not rolling back

From Dev

PetaPoco transaction not rolling back

From Dev

Cordova SQLite transaction not rolling back

From Dev

Rolling back transaction with Oracle OCCI

From Dev

Transaction in stored procedure not rolling back

From Dev

MySQL transaction not rolling back on error

From Dev

ActiveRecord transaction is not rolling back whole transaction

From Dev

Laravel DB::transaction not rolling back on exception

From Dev

Rolling back a transaction in apache chemistry cmis

From Dev

Rolling back transaction scope C#

From Dev

Rolling back a transaction in apache chemistry cmis

From Dev

What is a good way of rolling back a transaction in Postgres

From Dev

Kafka Transaction Manager sends to Kafka Broker despite transaction rolling back

From Dev

How to prevent cables from getting tangled in the wheels of a rolling rack?

From Dev

How to prevent history.back() from going back to login page

From Dev

SQL Server - Rolling back particular transaction only at a later date

From Dev

Exceptions when rolling back a transaction - connection already closed?

From Dev

Rolling back Transaction Doesn't Work with TpFIB Components

From Dev

Jms-message-driven channel adaper not rolling back the transaction

From Dev

Why is my MSDTC transaction not correctly rolling back on my localhost environment?

From Dev

How do I prevent sqlalchemy from creating a transaction on select?

From Dev

How to prevent from transaction to close at the catch(Exception e)?

From Dev

Rolling back Model-changes from action

Related Related

  1. 1

    How to prevent NServiceBus from rolling back the transaction or parts of it?

  2. 2

    How to prevent a specific INSERT statement from rolling back in a transaction

  3. 3

    How to prevent a specific INSERT statement from rolling back in a transaction

  4. 4

    Prevent inner transaction from rolling back outer transaction

  5. 5

    Prevent inner transaction from rolling back outer transaction

  6. 6

    TSQL: Prevent trigger suppressing error but rolling back transaction

  7. 7

    Rails transaction not rolling back

  8. 8

    PetaPoco transaction not rolling back

  9. 9

    Cordova SQLite transaction not rolling back

  10. 10

    Rolling back transaction with Oracle OCCI

  11. 11

    Transaction in stored procedure not rolling back

  12. 12

    MySQL transaction not rolling back on error

  13. 13

    ActiveRecord transaction is not rolling back whole transaction

  14. 14

    Laravel DB::transaction not rolling back on exception

  15. 15

    Rolling back a transaction in apache chemistry cmis

  16. 16

    Rolling back transaction scope C#

  17. 17

    Rolling back a transaction in apache chemistry cmis

  18. 18

    What is a good way of rolling back a transaction in Postgres

  19. 19

    Kafka Transaction Manager sends to Kafka Broker despite transaction rolling back

  20. 20

    How to prevent cables from getting tangled in the wheels of a rolling rack?

  21. 21

    How to prevent history.back() from going back to login page

  22. 22

    SQL Server - Rolling back particular transaction only at a later date

  23. 23

    Exceptions when rolling back a transaction - connection already closed?

  24. 24

    Rolling back Transaction Doesn't Work with TpFIB Components

  25. 25

    Jms-message-driven channel adaper not rolling back the transaction

  26. 26

    Why is my MSDTC transaction not correctly rolling back on my localhost environment?

  27. 27

    How do I prevent sqlalchemy from creating a transaction on select?

  28. 28

    How to prevent from transaction to close at the catch(Exception e)?

  29. 29

    Rolling back Model-changes from action

HotTag

Archive