事务标记为仅回滚

时间:2017-12-18 16:30:11

标签: java mysql spring hibernate transactions

我尝试更新数据库值时遇到此异常。我必须在指令之前将editing字段设置为TRUE,最后设置为FALSE,或者在出现异常时。 这是代码:

@Override
@SetEditingFleet
//  @UnSetEditingFleet
public void modifyFleet(User user, FleetForm fleetForm) throws Exception{
    databaseFleetsAndCarsServices.modifyFleet(user, fleetForm);
}

此方法调用@Async@Transactional方法,在某些情况下,我必须使用false值重置编辑fleet参数更新(在setEditingFleet中我将字段设置为true) 这是异步方法:

@Override
@Async
@Transactional(rollbackFor=Exception.class)
public void modifyFleet(User currentUser, FleetForm fleetForm) throws Exception {
    //Keep the progress status on DB
    fleetServices.setEditingProgress(fleetForm.getIdFleet(), "Start editing fleet");
    //SOme code
        //Keep the progress status on DB
        fleetServices.setEditingProgress(oldFleet.getIdFleet(), "");
//          throw new Exception("fleet has been restored Exception");
    }catch(Exception e){
        //Keep the progress status on DB
        fleetServices.setEditingProgress(oldFleet.getIdFleet(), "Sorry an error occured during the procedure, wait until restore is ended!");
        //Restore the file system procedure
        restoreProcedure(oldFleet.getIdFleet(), fleetFile, backupFolderFile);       
        //Keep the progress status on DB
        fleetServices.setEditingProgress(oldFleet.getIdFleet(), "");
        //Even with this exception set the fleet as not in editing
        utils.unSetEditingFleet(oldFleet.getIdFleet());
        throw new EditingException("fleet has been restored!");
    }
}

方法集编辑进度为@Transactional(propagation = Propagation.REQUIRES_NEW),因为我必须实时向用户显示此String。 即使unSetEditingFleet必须与REQUIRES_NEW一起使用,否则在发生错误时会回滚此数据库查询。 utils.unSetEditingFleet(Integer id)是一个不同的类,并有此说明:

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void unSetEditingFleet(Integer idFleet) throws QueryException {
    try {
        fleetServices.setEditingFleet(idFleet, false);   
        for(Car car : carServices.findByFleetIdFleet(idFleet)){
            carServices.setEditingCar(car.getIdCar(), false);   //Unset cars associated with the fleet 
        }    
    }catch(Exception e){
        throw new QueryException(e);
    }
}

当代码调用setEditingCar时,我有例外:

ERROR c.e.MyAsyncUncaughtExceptionHandler -
                Threw exception in async method Method Name:: modifyFleet org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:523)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:285)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
    at com.model.Utils$$EnhancerBySpringCGLIB$$fdeb7e66.unSetEditingFleet(<generated>)
    at com.services.dbservices.DatabaseFleetsAndCarsServicesImpl.modifyFleet(DatabaseFleetsAndCarsServicesImpl.java:628)
    at com.services.dbservices.DatabaseFleetsAndCarsServicesImpl$$FastClassBySpringCGLIB$$a9feae25.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:115)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:74)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
    ... 22 more

0 个答案:

没有答案