2017-02-20 68 views
1

中执行计划任务中的数据库事务任何人都可以举例说明如何使用Spring Boot 1.4.2.RELEASE在计划任务中调用数据库事务吗?非常感谢你。如何在Spring Boot 1.4.2.RELEASE

我使用Spring Boot 1.4.2.RELEASE通过@Scheduled(fixedRate = 5000) @Transactional(propagation = Propagation.REQUIRES_NEW)创建一个计划任务,并使用@Autowired获取JpaRepository的实例。

@Scheduled(fixedRate = 5000) 
@Transactional(propagation = Propagation.REQUIRES_NEW) 
public void importDataFromDat() { 
    List records = datUploadRecordRepository.findAllByImportTime(); 

    for (DatUploadRecord record : records) { 

     record.setImportTime(new Date()); 
     datUploadRecordRepository.save(record); 
    // datUploadRecordRepository.updateImportTime(new Date(), record.getId()); 
    } 
} 
  1. 它可以返回选择SQL的结果,但它不能实体保存到数据库中。
  2. 如果我在我自己的JpaRepository创建自己的更新方法,而不是调用save()方法时,会引起以下错误:

org.springframework.dao.InvalidDataAccessApiUsageException:执行 更新/删除查询;嵌套的异常是 javax.persistence.TransactionRequiredException:在 org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:413) 〜[弹簧ORM-4.3.4.RELEASE执行一个 更新/删除的查询。 jar:4.3.4.RELEASE] at org.springframework.orm.hibernate5.HibernateExceptionTranslator.translateExceptionIfPossible(HibernateExceptionTranslator.java:55) 〜[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) 〜[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.dao.support。 DataAccessUtils.translateIf必要(DataAccessUtils.java:213) 〜[弹簧-TX-4.3.4.RELEASE.jar:4.3.4.RELEASE]在 org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) 〜[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 〜[spring-aop-4.3.4 .RELEASE.jar:4.3.4.RELEASE]在 org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) 〜[弹簧 - 数据 - JP-A-1.10.5。 RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:17 (ExposeInvocationInterceptor.java:92) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at 4.3.4.RELEASE.jar:4.3.4.RELEASE]在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 〜[弹簧AOP-4.3.4.RELEASE.jar:4.3 .4.RELEASE]在 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) 〜[弹簧AOP-4.3.4.RELEASE.jar:4.3.4.RELEASE]在 融为一体。 sun.proxy。$ Proxy112.updateImportTime(Unknown Source)〜[na:na] at com.crane.schedules.ImportToDBFromDAT.importDataFromDat(ImportToDBFromDAT.java:65) 〜[classes /:na] at com.crane.schedules.ImportToDBFromDAT $$ FastClassBySpringCGLIB $$ 847b3ccb.invoke() 〜[类/:NA]在 org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 〜[弹簧-core-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation。invokeJoinpoint(CglibAopProxy.java:720) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99) 〜[spring-tx-4.3。 4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) 〜[spring-tx-4.3.4.RELEASE.jar:4.3.4 .RELEASE]在 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 〜[spring-tx-4.3 .4.RELEASE.jar:4.3.4.RELEASE]在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 〜[弹簧AOP-4.3.4.RELEASE.jar:4.3。 4.RELEASE] at org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at com .crane.schedules.ImportToDBFromDAT $$ EnhancerBySpringCGLIB $$ 971e70c9.importDataFromDat() 〜[类/:NA]在sun.reflect.NativeMethodAccessorImpl.invoke0(母语 方法)〜[NA:1.8.0_60]在 sun.reflect .NativeMethodAccessorImpl.invoke(Unknown Source) 〜[na:1.8.0_60] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 〜[na:1.8.0_60] at java.lang.reflect.Method.invoke(Unknown Source) 〜[na:1.8.0_60] at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java: 65) 〜[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) 〜[spring-context- 4.3.4.RELEASE.jar:4.3.4.RELEASE] java.util.concurrent.Executors $ RunnableAdapter.call(Unknown Source) [na:1.8.0_60] at java.util.concurrent.FutureTask.runAndReset(未知 来源)[na:1.8.0_60]在 java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 301(未知 )来源)[na:1.8.0_60] at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(Unknown Source)[na:1.8.0_60] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_60] at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source) [na:1.8.0_60] at java.lang.Thread.run(Unknown Source)[na:1.8。 0_60] 引起:javax.persistence.TransactionRequiredException:执行 一个更新/删除在 org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:54) 〜查询[冬眠-的EntityManager-5.0.11。 Final.jar:5.0.11.Final] at org.springframework.data.jpa.repository.query.JpaQueryExecuti on $ ModifyingExecution.doExecute(JpaQueryExecution.java:242) 〜[spring-data-jpa-1.10.5.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution .java:82) 〜[spring-data-jpa-1.10.5.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116) 〜 [spring-data-jpa-1.10.5.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106) 〜[spring-data-jpa- org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482) 〜[spring-data-commons-1.12.5。] 1.10.5.RELEASE.jar:na]RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460) 〜[spring-data-commons-1.12.5.RELEASE.jar:na ] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.data。 projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) 〜[spring-data-commons-1.12.5.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179 ) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.spring framework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99) 〜[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport。 invokeWithinTransaction(TransactionAspectSupport.java:282) 〜[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 〜[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 〜[spring-aop-4.3.4 .RELEASE.jar:4.3.4.RELEASE] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExcep tionTranslationInterceptor.java:136) 〜[弹簧TX-4.3.4.RELEASE.jar:4.3.4.RELEASE] ... 31个共同框架 省略

+0

我想说你的仓库的更新方法需要是Transactional。不是预定任务。 –

+0

谢谢你的回复。我使更新方法成为Transactional,但结果与上面相同。 –

+0

你的Spring引导是否正确地激活了事务管理? (即你是否在你的配置类中添加了注释'@ EnableAutoConfiguration' '@ EnableJpaRepositories' '@ EnableTransactionManagement') –

回答

0

也许晚了,但你能不能创建独立事务Component管理操作,然后从任务调用它?

在我们的应用程序中,我们使用了一个计划任务来清除文档。它调用文档服务来获取所有文档(事务操作),然后清除每个文档(从文档服务的清除方法也是事务性的)

相关问题