【问题标题】:springboot manually commit after each call to Stored Procedurespring boot 在每次调用存储过程后手动提交
【发布时间】:2020-12-01 06:28:20
【问题描述】:

我有一个 springboot 应用程序,它需要遍历大量记录并调用一个存储过程,该存储过程为每个记录读取插入一些数据到表中。

我们不能使用 BatchUpdate,因为处理数千条记录需要很长时间,我被要求频繁提交(在每条记录之后或 x 条记录之后)

我在网上看,我没有看到一个很好的例子来说明如何在 springboot 中手动提交,同时在循环中调用存储过程。 我正在使用 SimpleJdbcCall,我的代码如下所示:

@Transactional(isolation = Isolation.READ_UNCOMMITTED,propagation = Propagation.NOT_SUPPORTED)
public class EventsProcessor 
{

    @Autowired
    @Qualifier("dbDatasource")
    DataSource dataSource;

    public void process(List<Event> events) throws Exception
    {       
             SimpleJdbcCall dbTemplate = new SimpleJdbcCall(dataSource).withProcedureName("UPDATE_EVENTS").withSchemaName("TEST");
             DataSourceUtils.getConnection(dataSource).setAutoCommit(false);
             for (Event ev : events)
             {
                   //fill inParams here
                   outParams =  dbTemplate.execute(inParams);
                   DataSourceUtils.getConnection(dataSource).commit();
             }
    }


}

我尝试不使用 Propagation.NOT_SUPPORTED 并使用它,但结果相同。 代码正在执行对 sp 的调用,执行 commit() 时没有错误,但是在 commit() 之后,如果我查询 sp 插入记录的表,我看不到表中的记录.

如果我删除 setAutocommit(false) 和 commit 语句和 Propagation.NOT_SUPPORTED ,让 springboot 处理事务,然后在处理过程中,如果我执行 READ UNCOMMITTED,我可以看到表中的记录,但它们会在整个工作结束之前不要提交。 我做错了什么阻止提交在每一行之后发生?

【问题讨论】:

    标签: spring-boot transactions simplejdbccall


    【解决方案1】:

    我最终将对 SP 的调用分离为不同的方法

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    

    这样,当它从方法返回时,它会提交。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      相关资源
      最近更新 更多