java - Spring-JDBC batchUpdate 未提交更改。这是一个已知错误吗?

标签 java spring commit spring-jdbc batch-updates

我正在做的项目使用了以下依赖

<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 中的源代码似乎(因为驱动程序支持 批量更新) executeBatch()PreparedStatement 被调用。但是,我没有看到更新数据库的效果。

这是一个真正的错误还是我错过了这里明显的错误?如果已解决,请告知一个好的版本。 请注意,我需要一个不依赖于其他 Spring 模块(例如 Spring Core 或 MVC)的版本。提前致谢。

最佳答案

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

在Web 应用中,通常在服务层使用@Transactional 注解。在一个简单的应用程序中,Spring 提出了TransactionTemplate。这是Spring Reference Manual 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 是必须在事务上下文中调用的方法。

关于java - Spring-JDBC batchUpdate 未提交更改。这是一个已知错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24127175/

相关文章:

java - 在避免 java 中的 instanceof 运算符的同时观察多个可观察对象?

eclipse - Eclipse 中的大于符号在类旁边

java - Spring data JPA 不删除实体

oracle - 识别oracle程序中的非法提交

git - 如何使用带有 "git commit"的编辑器?

java - 错误 com.sap.xs2.security.commons.SAPOfflineTokenServices - 加载 JWT 库时出错

java - 如何在TitledBorder中找到标题的x位置

java - 为变量分配某个值时中断

java - 如何读取Spring Boot application.properties?

java - 如何使用 .htaccess 来加速 Spring 网站为不同类型的文件自定义浏览器缓存