2017-02-20 7 views
1

Springブート1.4.2を使用して、スケジュールされたタスクでDBトランザクションを呼び出す方法の例を教えてください。どうもありがとうございました。Springブート1.4.2.RELEASEでスケジュールされたタスクでデータベーストランザクションを作成する方法

私はSpringブート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の結果を返すことができますが、それはDBにエンティティを保存することはできません。
  2. 私はむしろsave()メソッドを呼び出すことよりも、自分のJpaRepositoryに自分のupdateメソッドを作成した場合、それが原因となり、以下のバグ:

org.springframework.dao.InvalidDataAccessApiUsageException: アップデートを実行します/削除クエリ。 org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:413) 〜[春-ORM-4.3.4.RELEASEで 更新/削除クエリを実行する:ネストされた例外は javax.persistence.TransactionRequiredExceptionです。 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] org.springframework.dao.supportにあります。 DataAccessUtils.translateIf必要(DataAccessUtils.java:213) 〜[spring-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] at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) 〜[spring-data-jpa-1.10.5。 RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:17) 9) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 〜[spring-aop- 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 .4RELEASE] org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at com。 $ Proxy112.updateImportTime(不明なソース)〜[na:na] com.crane.schedules.ImportToDBFromDAT.importDataFromDat(ImportToDBFromDAT.java:65) 〜[classes /:na] 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] にあるorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]の にあるorg.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99) 〜[spring-tx-4.3。 4.RELEASE.jar:4.3.4.RELEASE]で 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 .4RELEASE.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.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) 〜[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE] at com sun.reflectで.crane.schedules.ImportToDBFromDAT $$ $$ EnhancerBySpringCGLIB 971e70c9.importDataFromDat() 〜[クラス/::NA] sun.reflect.NativeMethodAccessorImpl.invoke0で(ネイティブ 方法)〜[1.8.0_60 NA] .NativeMethodAccessorImpl.invoke(不明なソース) 〜[na:1.8.0_60] at sun.reflect.DelegatingMethodAccessorImpl.invoke(不明なソース)[未知のソース] 〜[na:1.8.0_60] at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:〜[na:1.8.0_60] at java.lang.reflect.Method.invoke 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(不明なソース) [na:1.8.0_60] at java.util.concurrent.FutureTask.runAndReset(不明 ソース)[na:1.8.0_60] at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 301(不明な ソース)[na:1.8.0_60] at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(不明な ソース)[na:1.8.0_60] at java.util.concurrent.ThreadPoolExecutor.runWorker(不明なソース) [na:1.8.0_60] at java.util.concurrent.ThreadPoolExecutor $ Worker.run(不明なソース) [na:1.8.0_60] at java.lang.Thread.run(不明なソース)[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 $ ModifyingExecution.doExecute(JpaQueryExecution.java:242) 〜[spring-data-jpa-1.10.5.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.execute .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- 1.10.5.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482) 〜[spring-data-commons-1.12.5。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。 .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 (トランザクション・インターセプター・ジャンクション: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: 〜[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は、操作を管理し、それをタスクから呼び出しますか?

私たちのアプリケーションでは、スケジュールされたタスクを使用してドキュメントをパージしました。すべてのドキュメント(トランザクション操作)を取得し、各ドキュメントをパージするドキュメントサービスを呼び出します(ドキュメントサービスからのパージメソッドもトランザクションです)

関連する問題