UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only

后端 未结 2 472
时光说笑
时光说笑 2021-01-31 02:05

I have this scenario:

  1. fetch (read and delete) a record from IncomingMessage table
  2. read record content
  3. insert something to some t
相关标签:
2条回答
  • 2021-01-31 02:35

    This is the normal behavior and the reason is that your sqlCommandHandlerService.persist method needs a TX when being executed (because it is marked with @Transactional annotation). But when it is called inside processNextRegistrationMessage, because there is a TX available, the container doesn't create a new one and uses existing TX. So if any exception occurs in sqlCommandHandlerService.persist method, it causes TX to be set to rollBackOnly (even if you catch the exception in the caller and ignore it).

    To overcome this you can use propagation levels for transactions. Have a look at this to find out which propagation best suits your requirements.

    Update; Read this!

    Well after a colleague came to me with a couple of questions about a similar situation, I feel this needs a bit of clarification.
    Although propagations solve such issues, you should be VERY careful about using them and do not use them unless you ABSOLUTELY understand what they mean and how they work. You may end up persisting some data and rolling back some others where you don't expect them to work that way and things can go horribly wrong.


    EDIT Link to current version of the documentation

    0 讨论(0)
  • 2021-01-31 02:37

    The answer of Shyam was right. I already faced with this issue before. It's not a problem, it's a SPRING feature. "Transaction rolled back because it has been marked as rollback-only" is acceptable.

    Conclusion

    • USE REQUIRES_NEW if you want to commit what did you do before exception (Local commit)
    • USE REQUIRED if you want to commit only when all processes are done (Global commit) And you just need to ignore "Transaction rolled back because it has been marked as rollback-only" exception. But you need to try-catch out side the caller processNextRegistrationMessage() to have a meaning log.

    Let's me explain more detail:

    Question: How many Transaction we have? Answer: Only one

    Because you config the PROPAGATION is PROPAGATION_REQUIRED so that the @Transaction persist() is using the same transaction with the caller-processNextRegistrationMessage(). Actually, when we get an exception, the Spring will set rollBackOnly for the TransactionManager so the Spring will rollback just only one Transaction.

    Question: But we have a try-catch outside (), why does it happen this exception? Answer Because of unique Transaction

    1. When persist() method has an exception
    2. Go to the catch outside

      Spring will set the rollBackOnly to true -> it determine we must 
      rollback the caller (processNextRegistrationMessage) also.
      
    3. The persist() will rollback itself first.

    4. Throw an UnexpectedRollbackException to inform that, we need to rollback the caller also.
    5. The try-catch in run() will catch UnexpectedRollbackException and print the stack trace

    Question: Why we change PROPAGATION to REQUIRES_NEW, it works?

    Answer: Because now the processNextRegistrationMessage() and persist() are in the different transaction so that they only rollback their transaction.

    Thanks

    0 讨论(0)
提交回复
热议问题