Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different transaction name when using TransactionAspectSupport.currentTransactionStatus().transactionName #32310

Closed
rkudryashov opened this issue Feb 22, 2024 · 1 comment
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: documentation A documentation task
Milestone

Comments

@rkudryashov
Copy link

rkudryashov commented Feb 22, 2024

StackOverflow question

Spring Boot version: 3.2.1

If I use the following two services:

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

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

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    fun method1() {
        log.error("Name from TransactionAspectSupport: " + TransactionAspectSupport.currentTransactionStatus().transactionName)
        log.error("Name from TransactionSynchronizationManager: " + TransactionSynchronizationManager.getCurrentTransactionName())
        service2.method2()
    }
}

@Service
class Service2 {

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

    @Transactional
    fun method2() {
        log.error("Name from TransactionAspectSupport: " + TransactionAspectSupport.currentTransactionStatus().transactionName)
        log.error("Name from TransactionSynchronizationManager: " + TransactionSynchronizationManager.getCurrentTransactionName())
    }
}

the following is printed:

2024-02-22T13:15:35.240+03:00 ERROR 21308 --- [sb-repro] [nio-8080-exec-1] c.r.s.persistence.service.Service1       : Name from TransactionAspectSupport: com.romankudryashov.sbrepro.persistence.service.Service1.method1
2024-02-22T13:15:35.240+03:00 ERROR 21308 --- [sb-repro] [nio-8080-exec-1] c.r.s.persistence.service.Service1       : Name from TransactionSynchronizationManager: com.romankudryashov.sbrepro.persistence.service.Service1.method1
2024-02-22T13:15:35.240+03:00 ERROR 21308 --- [sb-repro] [nio-8080-exec-1] c.r.s.persistence.service.Service2       : Name from TransactionAspectSupport: com.romankudryashov.sbrepro.persistence.service.Service2.method2
2024-02-22T13:15:35.240+03:00 ERROR 21308 --- [sb-repro] [nio-8080-exec-1] c.r.s.persistence.service.Service2       : Name from TransactionSynchronizationManager: com.romankudryashov.sbrepro.persistence.service.Service1.method1

That is TransactionAspectSupport and TransactionSynchronizationManager behavior differs for method2. And TransactionAspectSupport mistakenly prints wrong transaction name.

If I remove @Transactional from method2, all is correct.

To reproduce, you can use: https://github.com/rkudryashov/sb-repro and call http://localhost:8080/test

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 22, 2024
@jhoeller
Copy link
Contributor

This is a consequence of the different meaning of "transaction" for those two calls: TransactionAspectSupport.currentTransactionStatus() exposes the locally declared transaction boundary as managed by the aspect (even if it ends up participating in an outer transaction at runtime where the local boundary is less relevant), whereas TransactionSynchronizationManager.getCurrentTransactionName() exposes the current (outer) resource transaction.

Since TransactionAspectSupport.currentTransactionStatus() is a rather internal API and shares behavior with TransactionCallback exposure in TransactionTemplate usage, the behavior should rather remain as it is. We need to document that difference though and suggest TransactionSynchronizationManager as the primary API to interact with the actual resource transaction.

@jhoeller jhoeller added in: data Issues in data modules (jdbc, orm, oxm, tx) type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 22, 2024
@jhoeller jhoeller added this to the 6.1.5 milestone Feb 22, 2024
@jhoeller jhoeller changed the title Wrong transaction name when using TransactionAspectSupport.currentTransactionStatus().transactionName Different transaction name when using TransactionAspectSupport.currentTransactionStatus().transactionName Feb 22, 2024
@jhoeller jhoeller self-assigned this Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants