Spring-JDBC batchUpdate不提交更改.这是已知的bug吗?



我正在进行的项目使用以下依赖项

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>3.2.0.RELEASE</version>
 </dependency>

用这个我做以下方法调用(1)

template.batchUpdate(INSERT_SQL, instance of BatchPreparedStatementSetter);

查看Spring JDBCTemplate中的源代码似乎(因为驱动程序支持)在PreparedStatement上调用executeBatch()。但是,我没有在数据库中看到更新的效果。

这是一个真正的错误还是我错过了明显的这里?如果这个问题已经解决了,请提供一个好的版本。请注意,我需要一个版本,没有依赖于其他Spring模块,如Spring Core或MVC。提前感谢。

如果您不想使用自动提交,则必须在Spring配置中设置PlatformTransactionManager。对于简单的JDBC使用,您可以使用DataSourceTransationManager

在web应用中,通常在服务层使用@Transactional注释。在一个简单的应用程序中,Spring提出了TransactionTemplate。下面是Spring参考手册3.2中的一个示例

public class SimpleService implements Service {
  // single TransactionTemplate shared amongst all methods in this instance
  private final TransactionTemplate transactionTemplate;
  // use constructor-injection to supply the PlatformTransactionManager
  public SimpleService(PlatformTransactionManager transactionManager) {
    Assert.notNull(transactionManager, "The 'transactionManager' argument must not be null.");
    this.transactionTemplate = new TransactionTemplate(transactionManager);
  }
  public Object someServiceMethod() {
    return transactionTemplate.execute(new TransactionCallback() {
      // the code in this method executes in a transactional context
      public Object doInTransaction(TransactionStatus status) {
        updateOperation1();
        return resultOfUpdateOperation2();
      }
    });
  }
}

updateOperation是必须在事务上下文中调用的方法。

1)你的事务管理设置/配置是什么?您如何开始并提交交易?在任何方法调用周围都有@Transactional注释吗?例如

@org.springframework.transaction.annotation.Transactional
public void doUpdate() {
  // jdbc template calls go here...
}

最新更新