Java 使用hibernate的Spring用户事务
我想自己控制hibernate事务,以便随时回滚。我调用一个线程来做生意,但不等待它完成它的工作并更新数据库。此更新仅在方法结束时可用,但我希望提交每个for循环中的更改,因此需要控制hibernate事务 我的示例代码如下:Java 使用hibernate的Spring用户事务,java,hibernate,spring,transactions,Java,Hibernate,Spring,Transactions,我想自己控制hibernate事务,以便随时回滚。我调用一个线程来做生意,但不等待它完成它的工作并更新数据库。此更新仅在方法结束时可用,但我希望提交每个for循环中的更改,因此需要控制hibernate事务 我的示例代码如下: for(BaseFileprocess fileProcess : unprocessedFiles) { BaseFileprocessfunctype functionType = fileProcessFunctionTypeService.
for(BaseFileprocess fileProcess : unprocessedFiles) {
BaseFileprocessfunctype functionType = fileProcessFunctionTypeService.findBySerno(fileProcess.getFunctioncodeserno());
if(functionType != null) {
taskExecutor.execute(new ServiceCallThread(functionType.getFunctionname(), fileProcess.getSerno(), fileProcess.getFilename()));
fileProcess.setStatu("1");
fileProcessService.update(fileProcess);//I need commit here
}
else {
System.out.println("There is no defined Function Type");
}
}
有什么建议吗?看看春天的天气。从文档中:
// 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();
}
});
}
这段代码不是很有趣,我们需要看看hibernate的工作原理。谢谢。交易模板适合我。