Transaction Attributes

你可以使用事务属性控制`isolation`、`propagation`和`timeout`设置。可以在 Spring core documentation中找到如何设置事务属性的更多信息。

You can use transaction attributes to control the isolation, propagation, and timeout settings. You can find more information on setting transaction attributes in the Spring core documentation.

Java

以下示例在 Java 中设置 isolationpropagationtimeout 事务属性:

The following example sets the isolation, propagation, and timeout transaction attributes in Java:

Java Configuration
@Bean
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
	DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
	attribute.setPropagationBehavior(Propagation.REQUIRED.value());
	attribute.setIsolationLevel(Isolation.DEFAULT.value());
	attribute.setTimeout(30);

	return new StepBuilder("step1", jobRepository)
				.<String, String>chunk(2, transactionManager)
				.reader(itemReader())
				.writer(itemWriter())
				.transactionAttribute(attribute)
				.build();
}
XML

以下示例在 XML 中设置 isolationpropagationtimeout transactionattributes:

The following example sets the isolation, propagation, and timeout transaction attributes in XML:

XML Configuration
<step id="step1">
    <tasklet>
        <chunk reader="itemReader" writer="itemWriter" commit-interval="2"/>
        <transaction-attributes isolation="DEFAULT"
                                propagation="REQUIRED"
                                timeout="30"/>
    </tasklet>
</step>