Ekaterina
Ekaterina

Reputation: 1892

@Transactional(propagation = Propagation.NOT_SUPPORTED) vs. no @Transactional annotation at all

Propagation.NOT_SUPPORTED. Execute non-transactionally, suspend the current transaction if one exists.

Is @Transactional(propagation = Propagation.NOT_SUPPORTED) the same as no @Transactional annotation at all in the following example, provided that m() is NOT called from another @Transactional method:

@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void m() {
        repo.insert(new B()); //insert() method is annotated with @Transactional(propagation = Propagation.REQUIRED)

}

Is a new transaction created for the insert() method? When should Propagation.NOT_SUPPORTED be used?

Upvotes: 5

Views: 5977

Answers (1)

Mohd Waseem
Mohd Waseem

Reputation: 1374

  1. Is @Transactional(propagation = Propagation.NOT_SUPPORTED) the same as no @Transactional?

    No, When we call m() from a method which have already started a transaction then it first suspends the current transaction and then do its work then after returning is resumes the suspended transaction. While in case of no @Transaction it will not suspend on-going transaction. check out following logs:

    o.s.orm.jpa.JpaTransactionManager.getTransaction - [ ] Creating new transaction with name [] o.h.e.t.internal.TransactionImpl.begin - [ ] begin . . o.s.orm.jpa.JpaTransactionManager.handleExistingTransaction - [ ] Suspending current transaction . . . o.s.orm.jpa.JpaTransactionManager.cleanupAfterCompletion - [ ] Resuming suspended transaction after completion of inner transaction

  2. Is a new transaction created for the insert() method?

    Yes, new transaction will begin for insert method.

    o.h.e.t.internal.TransactionImpl.begin - [ ] begin
    [ ] Initiating transaction commit
    [ ] committing

Note: Suspended transaction was resumed after insert() transaction was committed.

3.When should Propagation.NOT_SUPPORTED be used?

Not quite sure, you want the code to be run non-transactionally. If somehow you enter this code from context where transaction is, you suspend this transaction and continue non-transactionally.

Upvotes: 3

Related Questions