Skip to content

Commit 565ddcd

Browse files
committed
Change transaction manager type in default batch configuration
This commits changes the type of the transaction manager from `DataSourceTransactionManager` to `JdbcTransactionManager` in the default configuration of `@EnableBatchProcessing`. The `JdbcTransactionManager` adds common JDBC exception translation which is beneficial for Spring Batch to improve exception handling and error reporting. Resolves #4126
1 parent 3ef199b commit 565ddcd

File tree

82 files changed

+167
-166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+167
-166
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.springframework.batch.core.repository.JobRepository;
2727
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
2828
import org.springframework.beans.factory.InitializingBean;
29-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
29+
import org.springframework.jdbc.support.JdbcTransactionManager;
3030
import org.springframework.stereotype.Component;
3131
import org.springframework.transaction.PlatformTransactionManager;
3232
import org.springframework.util.Assert;
@@ -49,11 +49,11 @@ public class DefaultBatchConfigurer implements BatchConfigurer, InitializingBean
4949

5050
/**
5151
* Create a new {@link DefaultBatchConfigurer} with the passed datasource. This
52-
* constructor will configure a default {@link DataSourceTransactionManager}.
52+
* constructor will configure a default {@link JdbcTransactionManager}.
5353
* @param dataSource to use for the job repository and job explorer
5454
*/
5555
public DefaultBatchConfigurer(DataSource dataSource) {
56-
this(dataSource, new DataSourceTransactionManager(dataSource));
56+
this(dataSource, new JdbcTransactionManager(dataSource));
5757
}
5858

5959
/**

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@
117117
* </ul>
118118
*
119119
* The transaction manager provided by this annotation will be of type
120-
* {@link org.springframework.jdbc.datasource.DataSourceTransactionManager} configured
121-
* with the {@link javax.sql.DataSource} provided within the context.
120+
* {@link org.springframework.jdbc.support.JdbcTransactionManager} configured with the
121+
* {@link javax.sql.DataSource} provided within the context.
122122
*
123123
* In order to use a custom transaction manager, a custom {@link BatchConfigurer} should
124124
* be provided. For example:

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/DataSourceConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.context.annotation.Configuration;
2222
import org.springframework.core.io.ResourceLoader;
23-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
23+
import org.springframework.jdbc.support.JdbcTransactionManager;
2424
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2525
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
2626
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
@@ -50,8 +50,8 @@ public DataSource dataSource() {
5050
}
5151

5252
@Bean
53-
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
54-
return new DataSourceTransactionManager(dataSource);
53+
public JdbcTransactionManager transactionManager(DataSource dataSource) {
54+
return new JdbcTransactionManager(dataSource);
5555
}
5656

5757
}

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithBatchConfigurerTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3030
import org.springframework.context.annotation.Bean;
3131
import org.springframework.context.annotation.Configuration;
32-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
32+
import org.springframework.jdbc.support.JdbcTransactionManager;
3333
import org.springframework.test.util.AopTestUtils;
3434
import org.springframework.transaction.PlatformTransactionManager;
3535

@@ -45,10 +45,9 @@ public void testConfigurationWithDataSourceAndNoTransactionManager() throws Exce
4545
BatchConfigurer batchConfigurer = applicationContext.getBean(BatchConfigurer.class);
4646

4747
PlatformTransactionManager platformTransactionManager = batchConfigurer.getTransactionManager();
48-
Assert.assertTrue(platformTransactionManager instanceof DataSourceTransactionManager);
49-
DataSourceTransactionManager dataSourceTransactionManager = AopTestUtils
50-
.getTargetObject(platformTransactionManager);
51-
Assert.assertEquals(applicationContext.getBean(DataSource.class), dataSourceTransactionManager.getDataSource());
48+
Assert.assertTrue(platformTransactionManager instanceof JdbcTransactionManager);
49+
JdbcTransactionManager JdbcTransactionManager = AopTestUtils.getTargetObject(platformTransactionManager);
50+
Assert.assertEquals(applicationContext.getBean(DataSource.class), JdbcTransactionManager.getDataSource());
5251
Assert.assertSame(getTransactionManagerSetOnJobRepository(applicationContext.getBean(JobRepository.class)),
5352
platformTransactionManager);
5453
}

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithoutBatchConfigurerTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.springframework.context.annotation.Bean;
3232
import org.springframework.context.annotation.Configuration;
3333
import org.springframework.context.annotation.Primary;
34-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
34+
import org.springframework.jdbc.support.JdbcTransactionManager;
3535
import org.springframework.test.util.AopTestUtils;
3636
import org.springframework.transaction.PlatformTransactionManager;
3737

@@ -68,9 +68,9 @@ public void testConfigurationWithDataSourceAndNoTransactionManager() throws Exce
6868
BatchConfigurationWithDataSourceAndNoTransactionManager.class);
6969
PlatformTransactionManager platformTransactionManager = getTransactionManagerSetOnJobRepository(
7070
applicationContext.getBean(JobRepository.class));
71-
Assert.assertTrue(platformTransactionManager instanceof DataSourceTransactionManager);
72-
DataSourceTransactionManager dataSourceTransactionManager = (DataSourceTransactionManager) platformTransactionManager;
73-
Assert.assertEquals(applicationContext.getBean(DataSource.class), dataSourceTransactionManager.getDataSource());
71+
Assert.assertTrue(platformTransactionManager instanceof JdbcTransactionManager);
72+
JdbcTransactionManager JdbcTransactionManager = (JdbcTransactionManager) platformTransactionManager;
73+
Assert.assertEquals(applicationContext.getBean(DataSource.class), JdbcTransactionManager.getDataSource());
7474
}
7575

7676
@Test
@@ -81,10 +81,10 @@ public void testConfigurationWithDataSourceAndOneTransactionManager() throws Exc
8181
.getBean(PlatformTransactionManager.class);
8282
Assert.assertSame(transactionManager, platformTransactionManager);
8383
// In this case, the supplied transaction manager won't be used by batch and a
84-
// DataSourceTransactionManager will be used instead.
84+
// JdbcTransactionManager will be used instead.
8585
// The user has to provide a custom BatchConfigurer.
8686
Assert.assertTrue(getTransactionManagerSetOnJobRepository(
87-
applicationContext.getBean(JobRepository.class)) instanceof DataSourceTransactionManager);
87+
applicationContext.getBean(JobRepository.class)) instanceof JdbcTransactionManager);
8888
}
8989

9090
@Test
@@ -95,10 +95,10 @@ public void testConfigurationWithDataSourceAndMultipleTransactionManagers() thro
9595
.getBean(PlatformTransactionManager.class);
9696
Assert.assertSame(transactionManager2, platformTransactionManager);
9797
// In this case, the supplied primary transaction manager won't be used by batch
98-
// and a DataSourceTransactionManager will be used instead.
98+
// and a JdbcTransactionManager will be used instead.
9999
// The user has to provide a custom BatchConfigurer.
100100
Assert.assertTrue(getTransactionManagerSetOnJobRepository(
101-
applicationContext.getBean(JobRepository.class)) instanceof DataSourceTransactionManager);
101+
applicationContext.getBean(JobRepository.class)) instanceof JdbcTransactionManager);
102102
}
103103

104104
@Configuration

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,7 +53,7 @@
5353
import org.springframework.context.ConfigurableApplicationContext;
5454
import org.springframework.context.support.ClassPathXmlApplicationContext;
5555
import org.springframework.dao.DeadlockLoserDataAccessException;
56-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
56+
import org.springframework.jdbc.support.JdbcTransactionManager;
5757
import org.springframework.retry.RetryListener;
5858
import org.springframework.retry.listener.RetryListenerSupport;
5959
import org.springframework.test.util.ReflectionTestUtils;
@@ -297,7 +297,7 @@ public void testJobRepositoryDefaults() throws Exception {
297297
public void testTransactionManagerDefaults() throws Exception {
298298
ApplicationContext ctx = stepParserParentAttributeTestsCtx;
299299

300-
assertTrue(getTransactionManager("defaultTxMgrStep", ctx) instanceof DataSourceTransactionManager);
300+
assertTrue(getTransactionManager("defaultTxMgrStep", ctx) instanceof JdbcTransactionManager);
301301

302302
assertDummyTransactionManager("specifiedTxMgrStep", "dummyTxMgr", ctx);
303303

spring-batch-core/src/test/java/org/springframework/batch/core/job/ExtendedAbstractJobTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@
2828
import org.springframework.batch.core.repository.JobRepository;
2929
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
3030
import org.springframework.batch.core.step.StepSupport;
31-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
31+
import org.springframework.jdbc.support.JdbcTransactionManager;
3232
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
3333
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
3434
import org.springframework.lang.Nullable;
@@ -61,7 +61,7 @@ public void setUp() throws Exception {
6161
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
6262
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
6363
factory.setDataSource(embeddedDatabase);
64-
factory.setTransactionManager(new DataSourceTransactionManager(embeddedDatabase));
64+
factory.setTransactionManager(new JdbcTransactionManager(embeddedDatabase));
6565
factory.afterPropertiesSet();
6666
jobRepository = factory.getObject();
6767
job = new StubJob("job", jobRepository);

spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobFailureTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2021 the original author or authors.
2+
* Copyright 2008-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@
3131
import org.springframework.batch.core.repository.JobRepository;
3232
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
3333
import org.springframework.batch.core.step.StepSupport;
34-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
34+
import org.springframework.jdbc.support.JdbcTransactionManager;
3535
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
3636
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
3737

@@ -56,7 +56,7 @@ public void init() throws Exception {
5656
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
5757
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
5858
factory.setDataSource(embeddedDatabase);
59-
factory.setTransactionManager(new DataSourceTransactionManager(embeddedDatabase));
59+
factory.setTransactionManager(new JdbcTransactionManager(embeddedDatabase));
6060
factory.afterPropertiesSet();
6161
JobRepository jobRepository = factory.getObject();
6262
job.setJobRepository(jobRepository);

spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
5050
import org.springframework.batch.core.step.StepSupport;
5151
import org.springframework.batch.item.ExecutionContext;
52-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
52+
import org.springframework.jdbc.support.JdbcTransactionManager;
5353
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
5454
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
5555

@@ -97,7 +97,7 @@ public void setUp() throws Exception {
9797
EmbeddedDatabase embeddedDatabase = new EmbeddedDatabaseBuilder()
9898
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
9999
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").generateUniqueName(true).build();
100-
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(embeddedDatabase);
100+
JdbcTransactionManager transactionManager = new JdbcTransactionManager(embeddedDatabase);
101101
JobRepositoryFactoryBean repositoryFactoryBean = new JobRepositoryFactoryBean();
102102
repositoryFactoryBean.setDataSource(embeddedDatabase);
103103
repositoryFactoryBean.setTransactionManager(transactionManager);

spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleStepHandlerTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@
2929
import org.springframework.batch.core.repository.JobRepository;
3030
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
3131
import org.springframework.batch.core.step.StepSupport;
32-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
32+
import org.springframework.jdbc.support.JdbcTransactionManager;
3333
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
3434
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
3535

@@ -53,7 +53,7 @@ public void setUp() throws Exception {
5353
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
5454
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
5555
factory.setDataSource(embeddedDatabase);
56-
factory.setTransactionManager(new DataSourceTransactionManager(embeddedDatabase));
56+
factory.setTransactionManager(new JdbcTransactionManager(embeddedDatabase));
5757
factory.afterPropertiesSet();
5858
jobRepository = factory.getObject();
5959
jobExecution = jobRepository.createJobExecution("job", new JobParameters());

spring-batch-core/src/test/java/org/springframework/batch/core/job/builder/FlowJobBuilderTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@
5151
import org.springframework.context.annotation.Bean;
5252
import org.springframework.context.annotation.Configuration;
5353
import org.springframework.core.task.SimpleAsyncTaskExecutor;
54-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
54+
import org.springframework.jdbc.support.JdbcTransactionManager;
5555
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
5656
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
5757
import org.springframework.lang.Nullable;
@@ -114,7 +114,7 @@ public void init() throws Exception {
114114
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
115115
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
116116
factory.setDataSource(embeddedDatabase);
117-
factory.setTransactionManager(new DataSourceTransactionManager(embeddedDatabase));
117+
factory.setTransactionManager(new JdbcTransactionManager(embeddedDatabase));
118118
factory.afterPropertiesSet();
119119
jobRepository = factory.getObject();
120120
execution = jobRepository.createJobExecution("flow", new JobParameters());

spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobFailureTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2021 the original author or authors.
2+
* Copyright 2010-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@
3636
import org.springframework.batch.core.repository.JobRepository;
3737
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
3838
import org.springframework.batch.core.step.StepSupport;
39-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
39+
import org.springframework.jdbc.support.JdbcTransactionManager;
4040
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
4141
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4242

@@ -61,7 +61,7 @@ public void init() throws Exception {
6161
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
6262
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
6363
factory.setDataSource(embeddedDatabase);
64-
factory.setTransactionManager(new DataSourceTransactionManager(embeddedDatabase));
64+
factory.setTransactionManager(new JdbcTransactionManager(embeddedDatabase));
6565
factory.afterPropertiesSet();
6666
JobRepository jobRepository = factory.getObject();
6767
job.setJobRepository(jobRepository);

spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@
3838
import org.springframework.batch.core.repository.JobRepository;
3939
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
4040
import org.springframework.batch.core.step.StepSupport;
41-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
41+
import org.springframework.jdbc.support.JdbcTransactionManager;
4242
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
4343
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4444
import org.springframework.lang.Nullable;
@@ -78,7 +78,7 @@ public void setUp() throws Exception {
7878
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
7979
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
8080
factory.setDataSource(embeddedDatabase);
81-
factory.setTransactionManager(new DataSourceTransactionManager(embeddedDatabase));
81+
factory.setTransactionManager(new JdbcTransactionManager(embeddedDatabase));
8282
factory.afterPropertiesSet();
8383
this.jobRepository = factory.getObject();
8484
job.setJobRepository(this.jobRepository);

spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowStepTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@
3737
import org.springframework.batch.core.repository.JobRepository;
3838
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
3939
import org.springframework.batch.core.step.StepSupport;
40-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
40+
import org.springframework.jdbc.support.JdbcTransactionManager;
4141
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
4242
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4343

@@ -59,7 +59,7 @@ public void setUp() throws Exception {
5959
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
6060
JobRepositoryFactoryBean jobRepositoryFactoryBean = new JobRepositoryFactoryBean();
6161
jobRepositoryFactoryBean.setDataSource(embeddedDatabase);
62-
jobRepositoryFactoryBean.setTransactionManager(new DataSourceTransactionManager(embeddedDatabase));
62+
jobRepositoryFactoryBean.setTransactionManager(new JdbcTransactionManager(embeddedDatabase));
6363
jobRepositoryFactoryBean.afterPropertiesSet();
6464
jobRepository = jobRepositoryFactoryBean.getObject();
6565
jobExecution = jobRepository.createJobExecution("job", new JobParameters());

0 commit comments

Comments
 (0)