中執行計劃任務中的數據庫事務任何人都可以舉例說明如何使用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());
}
}
- 它可以返回選擇SQL的結果,但它不能實體保存到數據庫中。
- 如果我在我自己的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個共同框架 省略
我想說你的倉庫的更新方法需要是Transactional。不是預定任務。 –
謝謝你的回覆。我使更新方法成爲Transactional,但結果與上面相同。 –
你的Spring引導是否正確地激活了事務管理? (即你是否在你的配置類中添加了註釋'@ EnableAutoConfiguration' '@ EnableJpaRepositories' '@ EnableTransactionManagement') –