Search code examples
restejb

Same GET request to same URL give different Response, strange behavior


I have a very strange bug and I don't know what is going on.

I have an endpoints like this:

 private void init() {
    if (ioConfigurationDAO == null) {
      ioConfigurationDAO = new IOConfigurationDAO();
      ioConfigurationDAO.init();
    }

    property = new AigatewayProperty();
 }

  @GET
  @Path("/{id : \\d+}")
  @Produces(MediaType.APPLICATION_JSON)
  public Response getIoConfiguration(@PathParam("id") Integer id) {
    init();
    if (!ChannelName.isValidChannel(id)) {
      return Response.status(Response.Status.NOT_FOUND).build();
    }
    IOConfiguration ioConfig = ioConfigurationDAO.findIOConfiguration("CH" + id);
    System.out.println(ioConfig.getConversionType());
    // close();
    return Response.status(Response.Status.OK).entity(ioConfig).build();
  }

When I am making request to this url: http://localhost:8080/aigateway/rest/ioconfiguration/3

Some time, I get a correct response:

{"ioConfigurationId":"CH3","active":true,"name":"1","conversionType":"Linear","mInfo":0.32,"bInfo":0.55,"voltageDivide":"/4","sampleRange":"24 Bits","samplePeriod":10,"storeRaw":false,"storeConverted":false,"defaultGraph":"Line","title":"","unit":"","rangeLowerbound":0,"rangeUpperbound":100,"code":"function conversion_CH3 (input) {\n\treturn input;\n}"}

Sometimes, I get wrong response with null values:

{"ioConfigurationId":"CH3","active":null,"name":null,"conversionType":null,"mInfo":null,"bInfo":null,"voltageDivide":null,"sampleRange":null,"samplePeriod":null,"storeRaw":null,"storeConverted":null,"defaultGraph":null,"title":null,"unit":null,"rangeLowerbound":null,"rangeUpperbound":null,"code":null}

I don't know what is going on.

One thing to mention is that:

I have to manually initiate my entity manager becuase my EJB annotations doesn't work, and I am not closing my connection to the database since I don't know when the applicaton will end. I don't know if this is the reason that cause this problem.

Someone please help me, if you need more information, I am willing to share.

Thanks!


Solution

  • I solved this problem by using EJB properly. The only reason I can think that is causing this problem is because I never close the my entity manager, so the next time when I am trying to create a new one, there is some problem going on and that's why I am getting null values sometimes.