0

如何确定一个人是否batch正确使用模式?我正在使用带有 mybatis ( org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0)的 spring boot

设置

// Controller
@PutMapping("/{id}")
public Post updateById(@PathVariable Integer id, @RequestBody Post post) {
    return this.postService.updateById(id, post);
}

// PostMapper
@Update("UPDATE POST SET title = #{p.title}, body = #{p.body}, createdAt = #{p.createdAt}, createdBy = #{p.createdBy}, updatedAt = #{p.updatedAt}, updatedBy = #{p.updatedBy} WHERE id = #{id}")
public boolean updateById(@Param("id") Integer id, @Param("p") Post post);

@Flush
List<BatchResult> flush();

第一次测试 - 正常循环

所以我运行了update4 次查询

// Service
@Transactional
public Post updateById(Integer id, Post post) {
    for (int i = 1; i <= 4; i++) {
        this.postMapper.updateById(id++, post);
    }
        
    return post;
}

这是日志。对我来说看起来很正常,一一处理更新。

2021-10-31 22:44:48.542  INFO 1936 --- [nio-8080-exec-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-24 - Starting...
2021-10-31 22:44:48.606  INFO 1936 --- [nio-8080-exec-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-24 - Start completed.
2021-10-31 22:44:48.610 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Creating a new SqlSession
2021-10-31 22:44:48.610 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f]
2021-10-31 22:44:48.610 DEBUG 1936 --- [nio-8080-exec-2] o.m.s.t.SpringManagedTransaction         : JDBC Connection [HikariProxyConnection@1448464118 wrapping oracle.jdbc.driver.T4CConnection@26c16917] will be managed by Spring
2021-10-31 22:44:48.611 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==>  Preparing: UPDATE POST SET title = ?, body = ?, createdAt = ?, createdBy = ?, updatedAt = ?, updatedBy = ? WHERE id = ?
2021-10-31 22:44:48.611 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==> Parameters: batchtitle(String), batchbody(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 65(Integer)
2021-10-31 22:44:48.615 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : <==    Updates: 1
2021-10-31 22:44:48.615 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f]
2021-10-31 22:44:48.616 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f] from current transaction      
2021-10-31 22:44:48.616 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==>  Preparing: UPDATE POST SET title = ?, body = ?, createdAt = ?, createdBy = ?, updatedAt = ?, updatedBy = ? WHERE id = ?
2021-10-31 22:44:48.616 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==> Parameters: batchtitle(String), batchbody(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 66(Integer)
2021-10-31 22:44:48.619 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : <==    Updates: 1
2021-10-31 22:44:48.620 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f]
2021-10-31 22:44:48.620 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f] from current transaction      
2021-10-31 22:44:48.620 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==>  Preparing: UPDATE POST SET title = ?, body = ?, createdAt = ?, createdBy = ?, updatedAt = ?, updatedBy = ? WHERE id = ?
2021-10-31 22:44:48.620 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==> Parameters: batchtitle(String), batchbody(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 67(Integer)
2021-10-31 22:44:48.622 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : <==    Updates: 1
2021-10-31 22:44:48.623 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f]
2021-10-31 22:44:48.623 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f] from current transaction      
2021-10-31 22:44:48.623 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==>  Preparing: UPDATE POST SET title = ?, body = ?, createdAt = ?, createdBy = ?, updatedAt = ?, updatedBy = ? WHERE id = ?
2021-10-31 22:44:48.624 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==> Parameters: batchtitle(String), batchbody(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 68(Integer)
2021-10-31 22:44:48.626 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : <==    Updates: 1
2021-10-31 22:44:48.626 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f]
2021-10-31 22:44:48.626 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f]
2021-10-31 22:44:48.627 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f]
2021-10-31 22:44:48.627 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d4e918f]   

第二次测试——引入batchexecutor

创建一个不同beanbatch模式

@Configuration
class MapperConfig {

    // seem like once I create the `batchSqlSession`, I have to also create the default `sqlSessionTemplate`
    @Bean
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean("batchSqlSession")
    public SqlSessionTemplate sqlBatchSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
    }
}

然后在服务类

@Service
@AllArgsConstructor
public class PostService {
    private final PostMapper postMapper;
    @Qualifier("batchSqlSession") final SqlSessionTemplate batchSqlSession;
}

@Transactional
public Post updateById(Integer id, Post post) {
    int batchSize = 2;

    PostMapper pm = this.batchSqlSession.getMapper(PostMapper.class);
    
    for (int i = 1; i <= 4; i++) {
        pm.updateById(id++, post);

        if (i % batchSize == 0 || i == batchSize) {
            System.out.println("flushing batch");
            pm.flush();
        }
    }
}

当我运行它时,日志看起来并没有太大的不同,除了flushing batch中间的日志

2021-10-31 22:55:25.472  INFO 1936 --- [nio-8080-exec-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-29 - Starting...
2021-10-31 22:55:25.518  INFO 1936 --- [nio-8080-exec-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-29 - Start completed.
2021-10-31 22:55:25.521 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Creating a new SqlSession
2021-10-31 22:55:25.521 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090]
2021-10-31 22:55:25.521 DEBUG 1936 --- [nio-8080-exec-2] o.m.s.t.SpringManagedTransaction         : JDBC Connection [HikariProxyConnection@1375396778 wrapping oracle.jdbc.driver.T4CConnection@34ad8866] will be managed by Spring
2021-10-31 22:55:25.522 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==>  Preparing: UPDATE POST SET title = ?, body = ?, createdAt = ?, createdBy = ?, updatedAt = ?, updatedBy = ? WHERE id = ?
2021-10-31 22:55:25.522 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==> Parameters: batchtitle(String), batchbody(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 65(Integer)
2021-10-31 22:55:25.525 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : <==    Updates: 1
2021-10-31 22:55:25.525 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090]
2021-10-31 22:55:25.525 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090] from current transaction      
2021-10-31 22:55:25.525 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==>  Preparing: UPDATE POST SET title = ?, body = ?, createdAt = ?, createdBy = ?, updatedAt = ?, updatedBy = ? WHERE id = ?
2021-10-31 22:55:25.526 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==> Parameters: batchtitle(String), batchbody(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 66(Integer)
2021-10-31 22:55:25.527 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : <==    Updates: 1
2021-10-31 22:55:25.528 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090]
flushing batch
2021-10-31 22:55:25.528 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090] from current transaction      
2021-10-31 22:55:25.529 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090]
2021-10-31 22:55:25.529 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090] from current transaction      
2021-10-31 22:55:25.529 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==>  Preparing: UPDATE POST SET title = ?, body = ?, createdAt = ?, createdBy = ?, updatedAt = ?, updatedBy = ? WHERE id = ?
2021-10-31 22:55:25.529 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==> Parameters: batchtitle(String), batchbody(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 67(Integer)
2021-10-31 22:55:25.533 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : <==    Updates: 1
2021-10-31 22:55:25.533 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090]
2021-10-31 22:55:25.533 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090] from current transaction      
2021-10-31 22:55:25.534 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==>  Preparing: UPDATE POST SET title = ?, body = ?, createdAt = ?, createdBy = ?, updatedAt = ?, updatedBy = ? WHERE id = ?
2021-10-31 22:55:25.534 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : ==> Parameters: batchtitle(String), batchbody(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 2021-08-10T11:31:07.530686900(LocalDateTime), stackoverflow(String), 68(Integer)
2021-10-31 22:55:25.536 DEBUG 1936 --- [nio-8080-exec-2] c.b.s.s.post.PostMapper.updateById       : <==    Updates: 1
2021-10-31 22:55:25.536 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090]
flushing batch
2021-10-31 22:55:25.537 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090] from current transaction      
2021-10-31 22:55:25.537 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090]
2021-10-31 22:55:25.537 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090]
2021-10-31 22:55:25.537 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090]
2021-10-31 22:55:25.537 DEBUG 1936 --- [nio-8080-exec-2] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bb28090]   

那么是不是配置错误,因此,我实际上并没有使用该batch模式?

如果这不被认为是题外话,是否可以在创建batch-sizeapplication.properties或在配置批处理模式时配置默认值?SqlSessionTemplatebean

谢谢

4

0 回答 0