如何在 batchUpdate 期间覆盖 Spring JDBCTemplate 的事务超时值

How to override transaction timeout value for Spring JDBCTemplate during batchUpdate

我正在调用批处理 batchUpdate,它通常会在一次调用中发送超过 50 个查询,每个更新查询可能会更新 10,000 条记录。整个过程非常耗时,最终因事务超时异常而失败。 springContext.xml中定义的数据源通过@Qualifier注入JdbcTemplate,springContext.xml中定义的事务超时适用于整个应用程序,但在这个特定的事务中我想覆盖调用 batchUpdate 方法之前的事务超时值。

@Qualifier("myDataSource")
  @Autowired
  public void setDataSource(DataSource ds) {
    this.jdbc = new JdbcTemplate(ds);
    jdbc.setFetchSize(10000);
    jdbc.setQueryTimeout(0); // zero means there is no limit
  }

  //I want to set custom transaction timeout before calling this from some other methods.
  jdbc.batchUpdate(queries.toArray(new String[0]));

使用 @Transactional 注释指定超时。

@org.springframework.transaction.annotation.Transactional(timeout = 123) // 123 sec

Here为参考文档。