Spring Boot mandatory propagation for `Transactional` not working properly?

90 Views Asked by At

When there is a following call chain: rest method -> service1's transactional method (propagation=Requires_New) -> service2's transactional method (propagation=Mandatory):

@Service
class Service1(
    private val service2: Service2
) {

    private val log = LoggerFactory.getLogger(this.javaClass)

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    fun method1() {
        log.error("Transaction for method1: " + TransactionAspectSupport.currentTransactionStatus().transactionName)
        service2.method2()
    }
}


@Service
class Service2 {

    private val log = LoggerFactory.getLogger(this.javaClass)

    @Transactional(propagation = Propagation.MANDATORY)
    fun method2() {
        log.error("Transaction for method2: " + TransactionAspectSupport.currentTransactionStatus().transactionName)
    }
}

the log prints the following (the log is supposed to print the name of the current transaction for each method):

sb-repro     | 2024-02-20T13:20:25.000Z ERROR 1 --- [sb-repro] [   scheduling-1] c.r.s.persistence.service.Service1       : Transaction for method1: com.romankudryashov.sbrepro.persistence.service.Service1.method1
sb-repro     | 2024-02-20T13:20:25.000Z ERROR 1 --- [sb-repro] [   scheduling-1] c.r.s.persistence.service.Service2       : Transaction for method2: com.romankudryashov.sbrepro.persistence.service.Service2.method2

I think this is wrong (taking into account that Mandatory propagation should Support a current transaction, throw an exception if none exists) and the DB transaction for method2 should be com.romankudryashov.sbrepro.persistence.service.Service1.method1.

Am I wrong or is that a bug?

Reproducer is here: https://github.com/rkudryashov/sb-repro. You just need to call http://localhost:8080/test

(build native image is not necessary to reproduce.)

1

There are 1 best solutions below

0
Roman Q On