无法提交JPA事务 - RollbackException:事务标记为rollbackOnly [英] Can't commit JPA transaction - RollbackException: Transaction marked as rollbackOnly

查看:6707
本文介绍了无法提交JPA事务 - RollbackException:事务标记为rollbackOnly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我想说我已经在我的案例中看到了stackoverflow的所有主题,但无论如何都无法解决我的问题。

first I want to say that I have seen all the topics here on stackoverflow for my case but wasn't able to solve my problem anyway.

我需要每天运行计划任务以检查任务是否完成的天气 - 我这样做:

I need to run scheduled task every night to check weather the task was finished or not - I'm doing like this:

@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并自动连接几个@Transactional服务

CronJobService itself is @Transactional and autowires several @Transactional services

@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....
            }
        }
    }

流程没有错误,当所有交易都必须提交时,我收到这样的错误:

the process goes without errors and when all transactions must be committed I receive such error:

 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启动相同的功能它可以正常工作

I can't figure out why. Also If I launch same function from @Controller it works fine

@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";
    }
   }
}

所以我的问题是为什么这个函数从控制器工作 - 按预期提交所有内容而不是按计划任务工作(经过所有过程而没有错误)?

So my question is why this function works from controller - commits everything as expected and not works from scheduled task(goes through all process without errors)?

推荐答案

如果然后你可以把调试断点放在 org.springframework.transaction.interceptor.TransactionAspectSupport.completeTra nsactionAfterThrowing(TransactionInfo txInfo,Throwable ex)然后看看实际是什么例外是。

If you can then, put a debug break-point in org.springframework.transaction.interceptor.TransactionAspectSupport.completeTra‌​nsactionAfterThrowing(TransactionInfo txInfo, Throwable ex) and then see what the actual exception is.

这篇关于无法提交JPA事务 - RollbackException:事务标记为rollbackOnly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆