Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 希望多线程代码作为单个事务工作_Java_Multithreading_Transactions_Synchronization - Fatal编程技术网

Java 希望多线程代码作为单个事务工作

Java 希望多线程代码作为单个事务工作,java,multithreading,transactions,synchronization,Java,Multithreading,Transactions,Synchronization,我有一个for循环,其中存在一个具有多线程代码的方法(UpdateBancePnlForAllPermissions)。我的问题是我无法从UpdateBancePnlForAllPermissions()中删除多线程概念。但在这种特殊情况下,我希望updateBalancePnlForAllPermissions()保留其多线程特性。 我希望,即使for循环中的一个条目在此方法中失败,整个过程都应该停止,在db中创建的任何条目都应该恢复。 目前,如果一个条目发生故障,第二个条目的执行仍将继续。我

我有一个for循环,其中存在一个具有多线程代码的方法(UpdateBancePnlForAllPermissions)。我的问题是我无法从UpdateBancePnlForAllPermissions()中删除多线程概念。但在这种特殊情况下,我希望updateBalancePnlForAllPermissions()保留其多线程特性。 我希望,即使for循环中的一个条目在此方法中失败,整个过程都应该停止,在db中创建的任何条目都应该恢复。 目前,如果一个条目发生故障,第二个条目的执行仍将继续。我可以说,我将forloop作为一个事务来工作,一旦出现故障,整个事务就会回滚。有人能帮忙吗?我是多线程的新手,在这里感到迷茫

for (AppDate cobDate : recordsForCob.keySet()) {
    synchronized (BalanceServiceImpl.class) {
        PnlFile cobSpecificPnlFile
            = new PnlFile(pnlFile.getHeader(), pnlFile.getFooter(), Lists.newArrayList(recordsForCob.get(cobDate)));
        List<PnlBook> pnlBooks=getPnlBooks(cobSpecificPnlFile);
        try{
            updateBalancePnlForAllPermissions
                (cobDate, pnlFile.getHeader().getPnlCentre(), JEDI, pnlBooks, false, file);
        } catch(AlreadyRunningException exc){
            logger.error(exc);
        }catch(Exception e){
            logger.error(e);
            updatePnlUploadStatus(e.getMessage(), dirLocation,file);
            break;
       }    
for(AppDate-cobDate:recordsForCob.keySet()){
已同步(BalanceServiceImpl.class){
PnlFile cobSpecificPnlFile
=newpnlfile(PnlFile.getHeader(),PnlFile.getFooter(),Lists.newArrayList(recordsForCob.get(cobDate));
List pnlBooks=getPnlBooks(cobspectpnlfile);
试一试{
UpdateBancePnlForAllPermissions
(cobDate,pnlFile.getHeader().getPnlCentre(),JEDI,pnlBooks,false,file);
}捕获(ALREADYRUNNINGEXC异常){
记录器错误(exc);
}捕获(例外e){
错误(e);
UpdatePNUploadStatus(例如,getMessage(),dirLocation,file);
打破
}    

我相信您想要的是在一个事务中运行insert/update。这意味着您必须在循环之外启动一个事务,
updateBalancePnlForAllPermissions
和“UpdatePNUploadStatus”按要求运行/Requires\u NEW?您使用什么技术?.Spring?您能将整个方法标记/注释为事务性吗?同意对于Kenneth Clark,您需要一个事务。是否可以更改UpdateBancePnlForAllPermissions以使用事务?