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
This commit is contained in:
Mahmoud Ben Hassine
2022-05-15 07:07:31 +02:00
parent 3ef199b92b
commit 565ddcdf96
82 changed files with 167 additions and 166 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.datasource.SmartDataSource;
import org.springframework.transaction.PlatformTransactionManager;
@@ -183,7 +183,7 @@ public class ExtendedConnectionDataSourceProxyTests {
final ExtendedConnectionDataSourceProxy csds = new ExtendedConnectionDataSourceProxy();
csds.setDataSource(ds);
PlatformTransactionManager tm = new DataSourceTransactionManager(csds);
PlatformTransactionManager tm = new JdbcTransactionManager(csds);
TransactionTemplate tt = new TransactionTemplate(tm);
final TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@ import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
@@ -56,7 +56,7 @@ public class JdbcCursorItemReaderConfigTests {
when(ds.getConnection()).thenReturn(con);
when(ds.getConnection()).thenReturn(con);
con.commit();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
PlatformTransactionManager tm = new JdbcTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
final JdbcCursorItemReader<String> reader = new JdbcCursorItemReader<>();
reader.setDataSource(new ExtendedConnectionDataSourceProxy(ds));
@@ -88,7 +88,7 @@ public class JdbcCursorItemReaderConfigTests {
when(ds.getConnection()).thenReturn(con);
when(ds.getConnection()).thenReturn(con);
con.commit();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
PlatformTransactionManager tm = new JdbcTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
final JdbcCursorItemReader<String> reader = new JdbcCursorItemReader<>();
reader.setDataSource(ds);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010-2014 the original author or authors.
* Copyright 2010-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ import org.junit.runner.RunWith;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
@@ -61,7 +61,7 @@ public class StoredprocedureItemReaderConfigTests {
when(ds.getConnection()).thenReturn(con);
when(ds.getConnection()).thenReturn(con);
con.commit();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
PlatformTransactionManager tm = new JdbcTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
final StoredProcedureItemReader<String> reader = new StoredProcedureItemReader<>();
reader.setDataSource(new ExtendedConnectionDataSourceProxy(ds));
@@ -97,7 +97,7 @@ public class StoredprocedureItemReaderConfigTests {
when(ds.getConnection()).thenReturn(con);
when(ds.getConnection()).thenReturn(con);
con.commit();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
PlatformTransactionManager tm = new JdbcTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
final StoredProcedureItemReader<String> reader = new StoredProcedureItemReader<>();
reader.setDataSource(ds);
@@ -132,7 +132,7 @@ public class StoredprocedureItemReaderConfigTests {
when(ds.getConnection()).thenReturn(con);
when(ds.getConnection()).thenReturn(con);
con.commit();
PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
PlatformTransactionManager tm = new JdbcTransactionManager(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
final StoredProcedureItemReader<String> reader = new StoredProcedureItemReader<>();
reader.setDataSource(ds);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
@@ -190,7 +190,7 @@ public class StoredProcedureItemReaderBuilderTests {
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
JdbcTransactionManager transactionManager = new JdbcTransactionManager();
transactionManager.setDataSource(dataSource);

View File

@@ -31,7 +31,7 @@ import org.junit.runners.Parameterized.Parameters;
import org.springframework.batch.item.database.Order;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;
@@ -41,6 +41,7 @@ import static org.junit.Assert.assertEquals;
/**
* @author Henning Pöttker
* @author Mahmoud Ben Hassine
*/
@RunWith(Parameterized.class)
public class H2PagingQueryProviderIntegrationTests {
@@ -56,7 +57,7 @@ public class H2PagingQueryProviderIntegrationTests {
String connectionUrl = String.format("jdbc:h2:mem:%s;MODE=%s", UUID.randomUUID(), compatibilityMode);
DataSource dataSource = new SimpleDriverDataSource(new org.h2.Driver(), connectionUrl, "sa", "");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
PlatformTransactionManager transactionManager = new JdbcTransactionManager(dataSource);
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.executeWithoutResult(status -> {

View File

@@ -31,7 +31,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
@@ -50,6 +50,7 @@ import org.springframework.util.StringUtils;
* Java Application. Do the same any time you want to wipe the database and start again.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class DataSourceInitializer implements InitializingBean, DisposableBean {
@@ -125,7 +126,7 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
return;
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource));
TransactionTemplate transactionTemplate = new TransactionTemplate(new JdbcTransactionManager(dataSource));
transactionTemplate.execute(new TransactionCallback<Void>() {
@Override

View File

@@ -19,7 +19,7 @@
<property name="password" value="${batch.jdbc.password}" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
<bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager" lazy-init="true">
<property name="dataSource" ref="dataSource" />
</bean>

View File

@@ -8,7 +8,7 @@
<jdbc:script location="org/springframework/batch/item/database/init-foo-schema-hsqldb.sql"/>
</jdbc:embedded-database>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

View File

@@ -9,7 +9,7 @@
<property name="url" value="jdbc:hsqldb:mem:testdb" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

View File

@@ -13,7 +13,7 @@
<property name="url" value="jdbc:hsqldb:mem:testdb" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

View File

@@ -13,7 +13,7 @@
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="incrementerParent" class="org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer"

View File

@@ -17,7 +17,7 @@
<property name="dataDirectory" value="target/derby-home"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>