2016-03-02 164 views
0

您能否帮我解决这个问题。在这里,我使用AJAX调用将一些数据存储到使用JDO接口的Datastore。我将数据存储到数据存储并立即检索。在检索某些时候,它返回NULL作为响应(它不总是返回NULL,只有一些时候它返回NULL)。你能帮我解决这个问题吗?下面给出的代码被用于存储和检索数据使用JDO从数据存储中存储和检索数据

此代码,用于存储数据,

public void saveSchedule(String listName, String email, String date, String time, String details, String name) 
{ 

     Date hiredate = new Date(); 
     String gmtdate = hiredate.toGMTString(); 
     Schedule schedule = new Schedule(); 
     schedule.setName(name); 
     schedule.setListName(listName); 
     schedule.setEmail(email); 
     schedule.setDate(date); 
     schedule.setDateGMT(gmtdate); 
     schedule.setDetails(details); 
     schedule.setTime(time); 
     p = PMF.get().getPersistenceManager(); 
     try 
     { 
      p.makePersistent(schedule); 
     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
     } 
     finally 
     { 
      p.close(); 
     } 
    } 

此代码为检索数据,

public String savedDataRetrive(String details, String email) { 

     p = PMF.get().getPersistenceManager(); 
     Query q = p.newQuery(Schedule.class); 
     q.setFilter("details == '"+details+"' && email == '"+email+"'");  
     List<Schedule> sch = (List<Schedule>) q.execute(); 
     String data = null; 
     ObjectMapper n=new ObjectMapper(); 
     try { 
      data = n.writeValueAsString(sch); 

     } catch (JsonGenerationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JsonMappingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }finally{ 
      p.close(); 
     } 
     return data; 
    } 

回答

0

这里是一个有用的例子:

https://github.com/mattburns/OddPrints/blob/master/op-gae/src/com/oddprints/servlets/Edit.java#L89

@GET 
@Path("/basic/sample") 
@Produces(MediaType.TEXT_HTML) 
public Viewable loadBasicSample(@Context HttpServletRequest req) 
     throws FileUploadException, IOException, URISyntaxException { 

    return viewSampleImage(req, Settings.SAMPLE_PHOTO_BLOB_KEY, 
      Settings.SAMPLE_PHOTO_BLOB_SIZE, new URL(
        "http://www.oddprints.com/images/sample.jpg")); 

} 

Viewable viewSampleImage(HttpServletRequest req, Settings blobKeySetting, 
     Settings blobSizeSetting, URL image) throws MalformedURLException, 
     IOException { 
    String blobKeyString = ApplicationSetting.getSetting(blobKeySetting); 
    if (blobKeyString == null) { 

     InputStream imgStream = image.openStream(); 

     byte[] bytes = IOUtils.toByteArray(imgStream); 

     BlobKey blobKey = ImageBlobStore.INSTANCE.writeImageData(bytes); 
     blobKeyString = blobKey.getKeyString(); 
     ApplicationSetting.putSetting(blobKeySetting, blobKeyString); 
     ApplicationSetting.putSetting(blobSizeSetting, "" + bytes.length); 
    } 
    String blobSize = ApplicationSetting.getSetting(blobSizeSetting); 

    req.getSession().setAttribute("blobKeyString", blobKeyString); 
    req.getSession().setAttribute("blobSize", blobSize); 
    req.getSession().setAttribute("basicMode", Boolean.TRUE); 

    return viewBasic(req); 
} 
1

数据存储跨多个数据中心复制数据。这为读写提供了高水平的可用性,但是,大多数查询都是,最终一致。

最终一致性分布式 计算来实现高可用性是非正式保证 ,如果没有新的更新,以给定的数据项进行,最终所有 访问该项目将返回最后一个一致性模型更新的价值。

这很可能是您的查询有时不返回任何内容的原因。

我会建议你通过Structuring Data for Strong Consistency文章。

0

我会推荐使用memcache,这样取得的速度会更快,并且您将有更少的空对象返回IMO。