Skip to content

Commit 26c9d43

Browse files
fmbenhassinecppwfs
authored andcommitted
Change default transaction manager type to JdbcTransactionManager
This commits changes the type of the transaction manager from `DataSourceTransactionManager` to `JdbcTransactionManager` in the default configuration of `@EnableTask`. The `JdbcTransactionManager` adds common JDBC exception translation which is beneficial for Spring Cloud Task to improve exception handling and error reporting. Polishing
1 parent 7085cbc commit 26c9d43

File tree

6 files changed

+27
-22
lines changed

6 files changed

+27
-22
lines changed

docs/src/main/asciidoc/features.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ repository) to be used.
230230

231231
|`PlatformTransactionManager`
232232
|A transaction manager to be used when running updates for tasks.
233-
|`DataSourceTransactionManager` if a `DataSource` is used.
233+
|`JdbcTransactionManager` if a `DataSource` is used.
234234
`ResourcelessTransactionManager` if it is not.
235235
|===
236236

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
3232
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
3333
import org.springframework.context.ApplicationContext;
34-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
34+
import org.springframework.jdbc.support.JdbcTransactionManager;
3535
import org.springframework.orm.jpa.JpaTransactionManager;
3636
import org.springframework.transaction.PlatformTransactionManager;
3737

@@ -48,6 +48,7 @@
4848
*
4949
* @author Glenn Renfro
5050
* @author Michael Minella
51+
* @author Mahmoud Ben Hassine
5152
*/
5253
public class DefaultTaskConfigurer implements TaskConfigurer {
5354

@@ -144,7 +145,7 @@ public PlatformTransactionManager getTransactionManager() {
144145
}
145146
finally {
146147
if (this.transactionManager == null) {
147-
this.transactionManager = new DataSourceTransactionManager(this.dataSource);
148+
this.transactionManager = new JdbcTransactionManager(this.dataSource);
148149
}
149150
}
150151
}

spring-cloud-task-core/src/test/java/org/springframework/cloud/task/configuration/DefaultTaskConfigurerTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
/**
3838
* @author Glenn Renfro
39+
* @author Mahmoud Ben Hassine
3940
*/
4041
@ExtendWith(SpringExtension.class)
4142
@ContextConfiguration(classes = { EmbeddedDataSourceConfiguration.class })
@@ -65,20 +66,20 @@ public void testDefaultContext() throws Exception {
6566
DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(this.dataSource,
6667
TaskProperties.DEFAULT_TABLE_PREFIX, localContext);
6768
assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName())
68-
.isEqualTo("org.springframework.jdbc.datasource.DataSourceTransactionManager");
69+
.isEqualTo("org.springframework.jdbc.support.JdbcTransactionManager");
6970
}
7071

7172
@Test
7273
public void dataSourceTransactionManagerTest() {
7374
DefaultTaskConfigurer defaultTaskConfigurer = new DefaultTaskConfigurer(this.dataSource);
7475
assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName())
75-
.isEqualTo("org.springframework.jdbc.datasource.DataSourceTransactionManager");
76+
.isEqualTo("org.springframework.jdbc.support.JdbcTransactionManager");
7677
defaultTaskConfigurer = new DefaultTaskConfigurer(this.dataSource, "FOO", null);
7778
assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName())
78-
.isEqualTo("org.springframework.jdbc.datasource.DataSourceTransactionManager");
79+
.isEqualTo("org.springframework.jdbc.support.JdbcTransactionManager");
7980
defaultTaskConfigurer = new DefaultTaskConfigurer(this.dataSource, "FOO", this.context);
8081
assertThat(defaultTaskConfigurer.getTransactionManager().getClass().getName())
81-
.isEqualTo("org.springframework.jdbc.datasource.DataSourceTransactionManager");
82+
.isEqualTo("org.springframework.jdbc.support.JdbcTransactionManager");
8283
}
8384

8485
@Test

spring-cloud-task-core/src/test/java/org/springframework/cloud/task/configuration/RepositoryTransactionManagerConfigurationTests.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2020 the original author or authors.
2+
* Copyright 2020-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.
@@ -32,9 +32,9 @@
3232
import org.springframework.context.annotation.Bean;
3333
import org.springframework.context.annotation.Configuration;
3434
import org.springframework.jdbc.core.JdbcTemplate;
35-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
3635
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
3736
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
37+
import org.springframework.jdbc.support.JdbcTransactionManager;
3838
import org.springframework.test.jdbc.JdbcTestUtils;
3939
import org.springframework.test.util.ReflectionTestUtils;
4040
import org.springframework.transaction.PlatformTransactionManager;
@@ -44,6 +44,7 @@
4444

4545
/**
4646
* @author Michael Minella
47+
* @author Mahmoud Ben Hassine
4748
*/
4849
public class RepositoryTransactionManagerConfigurationTests {
4950

@@ -136,8 +137,8 @@ public DataSource dataSource() {
136137
}
137138

138139
@Bean
139-
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
140-
return new TestDataSourceTransactionManager(dataSource);
140+
public JdbcTransactionManager transactionManager(DataSource dataSource) {
141+
return new TestJdbcTransactionManager(dataSource);
141142
}
142143

143144
}
@@ -162,8 +163,8 @@ public DataSource dataSource() {
162163
}
163164

164165
@Bean
165-
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
166-
return new TestDataSourceTransactionManager(dataSource);
166+
public JdbcTransactionManager transactionManager(DataSource dataSource) {
167+
return new TestJdbcTransactionManager(dataSource);
167168
}
168169

169170
@Bean
@@ -172,15 +173,15 @@ public DataSource dataSource2() {
172173
}
173174

174175
@Bean
175-
public DataSourceTransactionManager transactionManager2(DataSource dataSource2) {
176-
return new DataSourceTransactionManager(dataSource2);
176+
public JdbcTransactionManager transactionManager2(DataSource dataSource2) {
177+
return new JdbcTransactionManager(dataSource2);
177178
}
178179

179180
}
180181

181-
private static class TestDataSourceTransactionManager extends DataSourceTransactionManager {
182+
private static class TestJdbcTransactionManager extends JdbcTransactionManager {
182183

183-
protected TestDataSourceTransactionManager(DataSource dataSource) {
184+
protected TestJdbcTransactionManager(DataSource dataSource) {
184185
super(dataSource);
185186
}
186187

spring-cloud-task-core/src/test/java/org/springframework/cloud/task/configuration/TestConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-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.
@@ -30,11 +30,12 @@
3030
import org.springframework.context.annotation.Bean;
3131
import org.springframework.context.annotation.Configuration;
3232
import org.springframework.core.io.ResourceLoader;
33-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
33+
import org.springframework.jdbc.support.JdbcTransactionManager;
3434
import org.springframework.transaction.PlatformTransactionManager;
3535

3636
/**
3737
* @author Michael Minella
38+
* @author Mahmoud Ben Hassine
3839
*/
3940

4041
@Configuration
@@ -74,7 +75,7 @@ public PlatformTransactionManager transactionManager() {
7475
return new ResourcelessTransactionManager();
7576
}
7677
else {
77-
return new DataSourceTransactionManager(this.dataSource);
78+
return new JdbcTransactionManager(this.dataSource);
7879
}
7980
}
8081

spring-cloud-task-core/src/test/java/org/springframework/cloud/task/repository/database/support/H2PagingQueryProviderTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@
3030
import org.springframework.batch.item.database.Order;
3131
import org.springframework.data.domain.PageRequest;
3232
import org.springframework.jdbc.core.JdbcTemplate;
33-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
3433
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
34+
import org.springframework.jdbc.support.JdbcTransactionManager;
3535
import org.springframework.transaction.PlatformTransactionManager;
3636
import org.springframework.transaction.support.TransactionTemplate;
3737

3838
import static org.assertj.core.api.Assertions.assertThat;
3939

4040
/**
4141
* @author Henning Pöttker
42+
* @author Mahmoud Ben Hassine
4243
*/
4344
class H2PagingQueryProviderTests {
4445

@@ -48,7 +49,7 @@ void testH2PagingQueryProvider(ModeEnum mode) {
4849
String connectionUrl = String.format("jdbc:h2:mem:%s;MODE=%s", UUID.randomUUID(), mode);
4950
DataSource dataSource = new SimpleDriverDataSource(new org.h2.Driver(), connectionUrl, "sa", "");
5051
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
51-
PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
52+
PlatformTransactionManager transactionManager = new JdbcTransactionManager(dataSource);
5253
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
5354

5455
transactionTemplate.executeWithoutResult(status -> {

0 commit comments

Comments
 (0)