我已经启动了一个线程,但是当它尝试访问实体管理器时,它会抛出异常 javax.enterprise.context.ContextNotActiveException: 当前线程
中不存在具有范围类型注释@RequestScoped的WebBeans上下文我应该如何使用线程访问EntityManger 任何解决方案?
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();
}
}
答案 0 :(得分:0)
如果服务上没有其他配置,您应该将服务作为托管bean并添加事务功能:
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class DefaultUserSevice