Skip to content

Commit 64f4da8

Browse files
committed
Align with breaking changes in latest Batch snapshots
Batch is now auto-configured to use the context's PlatformTransactionManager and DataSource or `@BatchDataSource`. When this does not meet the user's needs, they can use `@EnableBatchProcessing` or sub-class `DefaultBatchConfiguration` to take complete control with the auto-configuration backing off. Closes gh-32330
1 parent dc56fa6 commit 64f4da8

File tree

9 files changed

+101
-412
lines changed

9 files changed

+101
-412
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java

-158
This file was deleted.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java

+49-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import javax.sql.DataSource;
2020

2121
import org.springframework.batch.core.configuration.ListableJobLocator;
22+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
23+
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
2224
import org.springframework.batch.core.converter.JobParametersConverter;
2325
import org.springframework.batch.core.explore.JobExplorer;
2426
import org.springframework.batch.core.launch.JobLauncher;
@@ -35,13 +37,16 @@
3537
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3638
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
3739
import org.springframework.boot.autoconfigure.sql.init.OnDatabaseInitializationCondition;
40+
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
3841
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3942
import org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer;
4043
import org.springframework.context.annotation.Bean;
4144
import org.springframework.context.annotation.Conditional;
4245
import org.springframework.context.annotation.Configuration;
4346
import org.springframework.context.annotation.Import;
4447
import org.springframework.jdbc.datasource.init.DatabasePopulator;
48+
import org.springframework.transaction.PlatformTransactionManager;
49+
import org.springframework.transaction.annotation.Isolation;
4550
import org.springframework.util.StringUtils;
4651

4752
/**
@@ -60,11 +65,12 @@
6065
* @author Mahmoud Ben Hassine
6166
* @since 1.0.0
6267
*/
63-
@AutoConfiguration(after = HibernateJpaAutoConfiguration.class)
68+
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
6469
@ConditionalOnClass({ JobLauncher.class, DataSource.class, DatabasePopulator.class })
65-
@ConditionalOnBean({ DataSource.class, JobLauncher.class })
70+
@ConditionalOnBean({ DataSource.class, PlatformTransactionManager.class })
71+
@ConditionalOnMissingBean(value = DefaultBatchConfiguration.class, annotation = EnableBatchProcessing.class)
6672
@EnableConfigurationProperties(BatchProperties.class)
67-
@Import({ BatchConfigurerConfiguration.class, DatabaseInitializationDependencyConfigurer.class })
73+
@Import(DatabaseInitializationDependencyConfigurer.class)
6874
public class BatchAutoConfiguration {
6975

7076
@Bean
@@ -100,6 +106,46 @@ public SimpleJobOperator jobOperator(ObjectProvider<JobParametersConverter> jobP
100106
return factory;
101107
}
102108

109+
@Configuration(proxyBeanMethods = false)
110+
static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {
111+
112+
private final DataSource dataSource;
113+
114+
private final PlatformTransactionManager transactionManager;
115+
116+
private final BatchProperties properties;
117+
118+
SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider<DataSource> batchDataSource,
119+
PlatformTransactionManager transactionManager, BatchProperties properties) {
120+
this.dataSource = batchDataSource.getIfAvailable(() -> dataSource);
121+
this.transactionManager = transactionManager;
122+
this.properties = properties;
123+
}
124+
125+
@Override
126+
protected DataSource getDataSource() {
127+
return this.dataSource;
128+
}
129+
130+
@Override
131+
protected PlatformTransactionManager getTransactionManager() {
132+
return this.transactionManager;
133+
}
134+
135+
@Override
136+
protected String getTablePrefix() {
137+
String tablePrefix = this.properties.getJdbc().getTablePrefix();
138+
return (tablePrefix != null) ? tablePrefix : super.getTablePrefix();
139+
}
140+
141+
@Override
142+
protected Isolation getIsolationLevelForCreate() {
143+
Isolation isolation = this.properties.getJdbc().getIsolationLevelForCreate();
144+
return (isolation != null) ? isolation : super.getIsolationLevelForCreate();
145+
}
146+
147+
}
148+
103149
@Configuration(proxyBeanMethods = false)
104150
@Conditional(OnBatchDatasourceInitializationCondition.class)
105151
static class DataSourceInitializerConfiguration {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchConfigurerConfiguration.java

-74
This file was deleted.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java

+1-41
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.boot.context.properties.ConfigurationProperties;
2020
import org.springframework.boot.sql.init.DatabaseInitializationMode;
21+
import org.springframework.transaction.annotation.Isolation;
2122

2223
/**
2324
* Configuration properties for Spring Batch.
@@ -135,45 +136,4 @@ public void setInitializeSchema(DatabaseInitializationMode initializeSchema) {
135136

136137
}
137138

138-
/**
139-
* Available transaction isolation levels.
140-
*/
141-
public enum Isolation {
142-
143-
/**
144-
* Use the default isolation level of the underlying datastore.
145-
*/
146-
DEFAULT,
147-
148-
/**
149-
* Indicates that dirty reads, non-repeatable reads and phantom reads can occur.
150-
*/
151-
READ_UNCOMMITTED,
152-
153-
/**
154-
* Indicates that dirty reads are prevented; non-repeatable reads and phantom
155-
* reads can occur.
156-
*/
157-
READ_COMMITTED,
158-
159-
/**
160-
* Indicates that dirty reads and non-repeatable reads are prevented; phantom
161-
* reads can occur.
162-
*/
163-
REPEATABLE_READ,
164-
165-
/**
166-
* Indicate that dirty reads, non-repeatable reads and phantom reads are
167-
* prevented.
168-
*/
169-
SERIALIZABLE;
170-
171-
private static final String PREFIX = "ISOLATION_";
172-
173-
String toIsolationName() {
174-
return PREFIX + name();
175-
}
176-
177-
}
178-
179139
}

0 commit comments

Comments
 (0)