spring-projects / spring-projects/spring-batch

SYNCHRONIZATION_ON_ACTUAL_TRANSACTION not supported which leads to L1 cache problems in JPA

Open
#4,912 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: bug
Dominant language
Java
Stars
3k
Forks
2.5k
Avg merge
6d 53m
Merged PRs (30d)
3

Description

Bug description
When I setup Spring's TransactionManager to create synchronisation only on active transaction, then this error occurs when running spring batch job:
java.lang.IllegalStateException: Transaction synchronization is not active at org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(TransactionSynchronizationManager.java:277) ~[spring-tx-6.2.8.jar:6.2.8] at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:366) ~[spring-batch-core-5.2.2.jar:5.2.2] at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:307) ~[spring-batch-core-5.2.2.jar:5.2.2] at o

Why do we want to disable synchronisation?
Please see this bug : https://github.com/spring-projects/spring-data-jpa/issues/3943

Environment
Spring Boot 3.5.1, spring-batch-core 5.2.2

Steps to reproduce
Add to @Configuration these beans:

@Bean
    TransactionManagerCustomizer<JpaTransactionManager> transactionSynchronizationCustomizer() {
        return transactionManager ->
               transactionManager.setTransactionSynchronization(AbstractPlatformTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
    }
    @Bean
    TransactionManagerCustomizers platformTransactionManagerCustomizers(
            List<TransactionManagerCustomizer<?>> customizers) {
        return TransactionManagerCustomizers.of(customizers);
    }

And run any Tasklet Job.

Expected behavior
I should be able to disable synchronisation on Job Execution when transaction is not needed. There is unnecessary Hibernate session and L1 cache created that then gets corrupted with nested transactions during execution.

// Not providing a reproduction project as we can clearly see the need for active synchronisation in
TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:366)

Looking forward for a discussion!
Regards

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:366) and the TransactionSynchronizationManager.registerSynchronization call shown in the stack trace. Reproduce the failure with the SYNCHRONIZATION_ON_ACTUAL_TRANSACTION customizer and a Tasklet job, then verify that jobs can run without the exception while avoiding the unnecessary Hibernate session and L1 cache behavior described.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.