Spring - @Transactional - What happens in background?

前端 未结 6 583
逝去的感伤
逝去的感伤 2020-11-22 07:17

I want to know what actually happens when you annotate a method with @Transactional? Of course, I know that Spring will wrap that method in a Transaction.

6条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 08:02

    The simplest answer is:

    On whichever method you declare @Transactional the boundary of transaction starts and boundary ends when method completes.

    If you are using JPA call then all commits are with in this transaction boundary.

    Lets say you are saving entity1, entity2 and entity3. Now while saving entity3 an exception occur, then as enitiy1 and entity2 comes in same transaction so entity1 and entity2 will be rollback with entity3.

    Transaction :

    1. entity1.save
    2. entity2.save
    3. entity3.save

    Any exception will result in rollback of all JPA transactions with DB.Internally JPA transaction are used by Spring.

提交回复
热议问题