2016-03-02 166 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。