Add option to allow Spring Batch custom isolation levels with JPA

See original GitHub issue

Currently Spring Boot overrides Spring Batch default behaviour and specifies the connection-default isolation level for creating job metadata in the database (Spring Batch default is SERIALIZABLE with an option to override it).

This can cause issues where concurrent job executions can occur on the same database (our case).

So far the recommended way to workaround this was to override BasicBatchConfigurer (Spring Boot 1.x) or JpaBatchConfigurer (Spring Boot 2.x) and modify this method:

	@Override
	protected String determineIsolationLevel() {
		logger.warn("JPA does not support custom isolation levels, so locks may not be taken when launching Jobs");
		return "ISOLATION_DEFAULT";
	}

I propose to have a configuration property instead which can be used to disable this behaviour and allow Spring Batch to use custom isolation levels. For example, spring.batch.jpa.allow-custom-isolation-levels with false as default value which means keep the current behaviour.

Rationale: This behaviour was introduced in 2014 by this commit. Since then Spring Boot adopted HikariCP as default connection pool, which enables safe usage of custom isolation levels on top of JPA, because connections returned to HikariCP are properly cleaned up. Also, most JPA vendors allow this nowadays (see various JpaDialect implementations), however, not in all cases.

Since the connection pool can be changed to a less well-behaving one, also, not all JPA vendors support this, the default setting for the new property would be false. This would also ensure backwards compatibility.

Happy to contribute a PR for this enhancement request.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
fmbenhassinecommented, Nov 29, 2021

Thank you for this suggestion. What is the expected way to provide a custom isolation level when spring.batch.jpa.allow-custom-isolation-levels is set to true? If I understand correctly, the user would still need to extend JpaBatchConfigurer and override createJobRepository to provide the custom isolation level (unless one wants to keep the default value from Spring Batch). Is that correct?

My thinking is that if the goal is to avoid extending a class to override a property, why not make the isolation level configurable with a property like other Batch properties (table-prefix, schema, etc)? What do you think?

0reactions
snicollcommented, Jan 7, 2022

Closing in favor of #28859

Read more comments on GitHub >

github_iconTop Results From Across the Web

HibernateJpaDialect does not support custom isolation levels ...
i am relatively new to hibernate and spring batch. ... does not support custom isolation levels due to limitations in standard JPA.
Read more >
Transaction Propagation and Isolation in Spring @Transactional
It allows us to set propagation, isolation, timeout, read-only, and rollback conditions for our transaction. We can also specify the transaction manager.
Read more >
Spring Batch - Reference Documentation
Under option 1, adding new values means a manual reconfiguration of the batch or extract to ensure that the new value is added...
Read more >
Spring Boot - Transaction Isolation Tutorial - JavaInUse
In this Spring Boot tutorial we will be understanding what is transaction isolation and its different types and their behaviour.
Read more >
jpa transaction isolation level
In order to properly change the Spring transaction isolation level he have to define a custom JPA dialect that extends an already existing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found