2014-09-19 4 views
0

J'essaie d'exécuter une fonction à l'aide de l'annotation @Scheduled. Qui, à son tour, appelle une sauvegarde de référentiel. Mais l'enregistrement échoue avec InvalidDataAccessApiUsageException. Poster ma trace de pile.InvalidDataAccessApiUsageException: Aucune requête liée au thread trouvée:

org.springframework.dao.InvalidDataAccessApiUsageException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381) 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:156) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) 
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) 
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
    at com.sun.proxy.$Proxy87.save(Unknown Source) 
    at com.ecsite.web.report.ReportService.saveAttachmentDocument(ReportService.java:495) 
    at com.ecsite.web.mail.MailAttachmentProcessor1.processMailAttachment(MailAttachmentProcessor1.java:97) 
    at com.ecsite.web.mail.MailAttachmentProcessor1.processTask(MailAttachmentProcessor1.java:59) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) 
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) 
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:724) 
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) 
    at com.ecsite.web.audit.AuditService.getContext(AuditService.java:93) 
    at com.ecsite.web.entity.BaseEntity.getAuditService(BaseEntity.java:167) 
    at com.ecsite.web.entity.BaseEntity.postPersist(BaseEntity.java:181) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.hibernate.ejb.event.BeanCallback.invoke(BeanCallback.java:39) 
    at org.hibernate.ejb.event.EntityCallbackHandler.callback(EntityCallbackHandler.java:110) 
    at org.hibernate.ejb.event.EntityCallbackHandler.postCreate(EntityCallbackHandler.java:83) 
    at org.hibernate.ejb.event.EJB3PostInsertEventListener.onPostInsert(EJB3PostInsertEventListener.java:49) 
    at org.hibernate.action.internal.EntityIdentityInsertAction.postInsert(EntityIdentityInsertAction.java:149) 
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:102) 
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377) 
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:214) 
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194) 
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178) 
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321) 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286) 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192) 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) 
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78) 
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208) 
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151) 
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78) 
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:853) 
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:827) 
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:831) 
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:875) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:257) 
    at com.sun.proxy.$Proxy66.persist(Unknown Source) 
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:358) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:344) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:329) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 
    ... 25 more 

J'ai ajouté le "RequestContextListener" dans le fichier web.xml. et faire toutes les configurations en utilisant java (par programmation).

// Classe dans laquelle le planificateur est appelé.

@Scheduled(cron = "0 0/5 * 1/1 * *") 
public void processTask() { 
    saveEntity(); 
} 

// service

public saveEntity(){ 
Media media = new Media(); 
//setting values to media 
media = fieldMediaRepository.save(media); 
} 

// Entité Classe

class Media{ 

    private String property1; 
    private String property1; 
    private String property1; 

    @PostPersist 
    public void postPersist() { 
     (AuditService)AuditService.getContext().getBean("AuditService"); 
    } 
} 

// AuditService Classe

class AuditService{ 
    public static WebApplicationContext getContext() { 
     if (context == null) { 
      ServletRequestAttributes sessAttributes = 
        (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes(); 
      context = 
        WebApplicationContextUtils       .getWebApplicationContext(sessAttributes.getRequest().getServletContext()); 
     } 
     return context; 
    } 
} 

Afficher la trace:

//Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) 
    at com.ecsite.web.audit.AuditService.getContext(AuditService.java:93) 
+2

Code postal de la classe qui contient votre méthode @Scheduled. –

+0

Comment puis-je accéder à l'objet contextuel, lorsque je ne fais PAS une demande du navigateur. –

+0

La classe qui contient la méthode @Scheduled est-elle un bean Spring? Avez-vous mentionné cette classe comme «haricot» au printemps config OU avez-vous mentionné cette classe comme «@Component»? –

Répondre

0

Le code pourrait être utile, mais la trace de la pile me dit que vous essayez d'accéder à la demande avec un RequestContextHolder.

Le problème ici pourrait être que le RequestContextListener a lié la requête au thread CURRENT. Et vous essayez d'y accéder dans un autre thread (parce que le Scheduler crée un autre thread).

0

Il semble que vous exécutiez du code hors du contexte Springs. Votre méthode @Scheduled n'a pas accès au référentiel qui est dans le contexte de Spring.

Créer une classe avec la méthode @Scheduled, Composant de Spring. Vous pouvez soit entrer des beans dans votre configuration, soit utiliser l'annotation @Component pour votre classe. Cela devrait le faire.

Pas besoin d'utiliser 'RequestContextListener' dans web.xml.

Questions connexes