Annotation @Transactional. How to rollback?

后端 未结 4 1973
长情又很酷
长情又很酷 2020-11-28 02:53

I used this annotation successfully for a Dao class. And rollback works for tests.

But now I need to rollback real code, not just tests. There are special annotatio

4条回答
  •  隐瞒了意图╮
    2020-11-28 03:20

    Just throw any RuntimeException from a method marked as @Transactional.

    By default all RuntimeExceptions rollback transaction whereas checked exceptions don't. This is an EJB legacy. You can configure this by using rollbackFor() and noRollbackFor() annotation parameters:

    @Transactional(rollbackFor=Exception.class)
    

    This will rollback transaction after throwing any exception.

提交回复
热议问题