2016-02-12 78 views
2

首先我想說我已經在我的案例中看到了所有的主題,但無法解決我的問題。無法提交JPA事務 - RollbackException:標記爲rollbackOnly的事務

我需要每天晚上運行調度的任務,檢查天氣的任務是完成與否 - 我在做這樣的:

@Service 
@Transactional 
public class CronBackGroundProcess { 

@Autowired 
private CronJobService cronJobService; 

@Scheduled(cron = "15 01 01 ? * *") 
public void StartNightJob() { 
    CronJobLog log = new CronJobLog(); 
    int count = 0; 
    try { 
     log.setStartTime(new Date()); 
     log.setStatus("Entered StartNightJob Function"); 
     cronJobService.saveCronJobLog(log); 

     List<Task> Tasks = cronJobService.getActive_AND_InArreasTasks(); 
     log.setStatus("Grabbed List of tasks to Check"); 
     cronJobService.saveCronJobLog(log); 

     for (Task Task : Tasks) { 
      cronJobService.StartNightJobProcess(Task, true); 
      count++; 
     } 
    } catch (Exception e) { 
     CronJobLog log2 = new CronJobLog(); 
     log2.setStatus("Error Occurred " + new Date().toString() + e.getMessage()); 
     cronJobService.saveCronJobLog(log2); 
    } 

    log.setLoansChecked(count); 
    log.setStatus("Finished"); 
    log.setEndDate(new Date()); 
    cronJobService.saveCronJobLog(log); 
} 
} 

CronJobService本身@Transactional和autowires幾個@Transactional服務

@Service 
@Transactional 
public class CronJobService { 

@Autowired 
private ProductService productService; 

@Autowired 
private RepaymentService repaymentService; 

@Autowired 
private CronJobLogDAO cronJobLogDAO; 


@Autowired 
private TransferService transferService; 

public String StartNightJobProcess(Account account, boolean makeTransfers) { 

    do something.... 
      } 
     } 
    } 

過程不用錯誤,並在所有交易必須承諾我收到這樣的錯誤:

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:524) ~[spring-orm-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) ~[spring-tx-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) ~[spring-tx-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478) ~[spring-tx-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272) ~[spring-tx-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) ~[spring-tx-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646) ~[spring-aop-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at ge.shemo.services.core.CronBackGroundProcess$$EnhancerByCGLIB$$30cdcf31.StartNightJob(<generated>) ~[spring-core-4.0.0.RELEASE.jar:na] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79] 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79] 
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_79] 
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) ~[spring-context-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) [spring-context-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [na:1.7.0_79] 
at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_79] 
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178) [na:1.7.0_79] 
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292) [na:1.7.0_79] 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_79] 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_79] 
at java.lang.Thread.run(Thread.java:745) [na:1.7.0_79] 
Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly 
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:58) ~[hibernate-entitymanager-5.0.1.Final.jar:5.0.1.Final] 
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:515) ~[spring-orm-4.0.0.RELEASE.jar:4.0.0.RELEASE] 
... 22 common frames omitted 

我不明白爲什麼。 另外,如果我從@Controller推出相同的功能,它工作正常

@Controller 
@RequestMapping("/test") 
public class test { 

@Autowired 
private ClientService clientService; 

@Autowired 
private CronBackGroundProcess cronBackGroundProcess; 

@RequestMapping(value = "/test") 
@ResponseBody 
public void test() throws Exception { 
    try { 

     cronBackGroundProcess.StartNightJob(); 
    } catch (Exception e) { 
     String s = "sd"; 
    } 
    } 
} 

所以我的問題是,爲什麼這個功能從控制器的工作原理 - 預期和計劃任務未提交作品的一切(通過全過程不用錯誤)?

+0

這是完整的堆棧跟蹤嗎? – Kayaman

+0

是的,它是完整的堆棧跟蹤 – Irakli

+0

你的事務標記爲rollbackOnly,因爲之前發生了一個錯誤,破壞了連接。仔細查看日誌以查找異常,如果需要的話甚至可以啓用額外的日誌記錄(只有在某處發現該異常並且未打印時才需要)。 –

回答

5

如果可以的話,請在org.springframework.transaction.interceptor.TransactionAspectSupport.completeTra‌​nsactionAfterThrowing(TransactionInfo txInfo, Throwable ex)中放置一個調試斷點,然後查看實際的異常情況。

0

你不需要標記CronBackGroundProcess作爲@Transactional,因爲在StartNightJob()方法你沒有訪問數據庫的所有訪問數據庫,因爲我猜你在CronJobService執行。

因此請從CronBackGroundProcess刪除@Transactional,它必須幫助。

相關問題