我正在做的项目使用了以下依赖
<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/