|
19 | 19 | import javax.sql.DataSource;
|
20 | 20 |
|
21 | 21 | 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; |
22 | 24 | import org.springframework.batch.core.converter.JobParametersConverter;
|
23 | 25 | import org.springframework.batch.core.explore.JobExplorer;
|
24 | 26 | import org.springframework.batch.core.launch.JobLauncher;
|
|
35 | 37 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
36 | 38 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
37 | 39 | import org.springframework.boot.autoconfigure.sql.init.OnDatabaseInitializationCondition;
|
| 40 | +import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration; |
38 | 41 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
39 | 42 | import org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer;
|
40 | 43 | import org.springframework.context.annotation.Bean;
|
41 | 44 | import org.springframework.context.annotation.Conditional;
|
42 | 45 | import org.springframework.context.annotation.Configuration;
|
43 | 46 | import org.springframework.context.annotation.Import;
|
44 | 47 | import org.springframework.jdbc.datasource.init.DatabasePopulator;
|
| 48 | +import org.springframework.transaction.PlatformTransactionManager; |
| 49 | +import org.springframework.transaction.annotation.Isolation; |
45 | 50 | import org.springframework.util.StringUtils;
|
46 | 51 |
|
47 | 52 | /**
|
|
60 | 65 | * @author Mahmoud Ben Hassine
|
61 | 66 | * @since 1.0.0
|
62 | 67 | */
|
63 |
| -@AutoConfiguration(after = HibernateJpaAutoConfiguration.class) |
| 68 | +@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class }) |
64 | 69 | @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) |
66 | 72 | @EnableConfigurationProperties(BatchProperties.class)
|
67 |
| -@Import({ BatchConfigurerConfiguration.class, DatabaseInitializationDependencyConfigurer.class }) |
| 73 | +@Import(DatabaseInitializationDependencyConfigurer.class) |
68 | 74 | public class BatchAutoConfiguration {
|
69 | 75 |
|
70 | 76 | @Bean
|
@@ -100,6 +106,46 @@ public SimpleJobOperator jobOperator(ObjectProvider<JobParametersConverter> jobP
|
100 | 106 | return factory;
|
101 | 107 | }
|
102 | 108 |
|
| 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 | + |
103 | 149 | @Configuration(proxyBeanMethods = false)
|
104 | 150 | @Conditional(OnBatchDatasourceInitializationCondition.class)
|
105 | 151 | static class DataSourceInitializerConfiguration {
|
|
0 commit comments