0

I have started a thread but when it try to access entity Manager it throws exception as javax.enterprise.context.ContextNotActiveException: WebBeans context with scope type annotation @RequestScoped does not exist within current thread

How should I access EntityManger with thread any solution?

 public class SchedulerForData {
        @Inject
        private DefaultUserSevice userSevice;


        public void beepForAnHour() {
            long initialDelay;
            final Runnable beeper = new Runnable() {
                public void run() {
                         userSevice.getData();
                         }
            scheduler.scheduleAtFixedRate(beeper, 60, 30, TimeUnit.SECONDS);

        }
        }
    }



    public class DefaultUserSevice {

    @Inject
    EntityManager entityManger;


    public void getData(){
      List resultList = entityManager.createNativeQuery("Select * from USER").getResultList();
    }

    }
AmolG
  • 53
  • 8

1 Answers1

0

If there is no other configuration on the service, you should make the service a managed bean and add transactional feature:

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class DefaultUserSevice
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • I used above annotation but it throws exception at entityManger.createNativeQuery() but I cannot see any kind of exception or error on console – AmolG Oct 08 '17 at 14:56
  • No sir because its cannot proceed ahead – AmolG Oct 08 '17 at 15:03