Issue
Cannot change the ExecutorType when there is an existing transaction in _____Processor.
1. Set a Consistent ExecutorType
Ensure that the ExecutorType is consistently set across all MyBatis operations. Typically, ExecutorType.BATCH is used for batch processing, but you can choose the appropriate one for your use case (SIMPLE, REUSE, BATCH).
In runtime, Spring Batch was excuted with ExecuteType.Batch, but annotation-base queries will be excuted with ExecuteType.SIMPLE, which is having differentition point.
1.1 Trouble Shooting and Solution
Implement MyBatisConfig with returning SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
Let's show e.g
import org.apache.ibatis.session.ExecutorType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
@MapperScan("your.package.com.model.mapper")
public class MyBatisConfig {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
// Ensure that the mapper XML files are found
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/*.xml")
);
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory, ExecutorType.SIMPLE);
}
}
@MapperScan Annotation could be matched with interface of mapper location. if non-correct path was assigned, mapper interface do not find real object.
setMapperLocation method will be matched with mybatis.mapper-location in application.yml.
So, We need to match mapper-location path like classpath:*mappers/*.xml
Since I use absolute path in application.yml, which is unusuall case, classpath solution could not be fitted.
In theory, we would use classpath with ExecutorType.SIMPLE.
[Concenturate to understand issue environment]
MyBatisPageReaderBuilder will return ExecutorType.Batch, and this part causes Spring Batch ExecutorType not to be matched issue.
@Bean
@StepScope
public MyBatisCursorItemReader<______Dto__or__Entity> ___ItemReader(
@Value("#{jobParameters[UUID]}") String uuid) throws Exception {
Map<String, Object> parameterValues = new HashMap<>();
parameterValues.put("uuid", uuid);
MyBatisCursorItemReaderBuilder<YourDto> readerBuilder = new MyBatisCursorItemReaderBuilder<>();
readerBuilder.sqlSessionFactory(sqlSessionFactory);
readerBuilder.queryId("your.package.com.model.mapper.InterfaceMapper.select");
readerBuilder.parameterValues(parameterValues);
return readerBuilder.build();
}
Quote
https://bkjeon1614.github.io/blog/java-springbatch-7
'Java' 카테고리의 다른 글
Pattern and Matcher (0) | 2024.07.16 |
---|---|
SecureRandom (0) | 2024.07.12 |
[MyBatis] Page with MyBatis (0) | 2024.07.01 |
[Spring Security] Public vs Private method to be applied Spring Security (1) | 2024.06.07 |
[method] java stream mapToObj (0) | 2024.05.07 |