Reputation: 1
Can you please help me to solve this problem. Here I'm storing some data to Datastore with JDO Interface using AJAX call. I'm storing the data to datastore and Retrieving it immediately. While Retrieving some times it returns NULL as a response(Its not always returning NULL. Only some times it returning NULL). Can you please help me to fix this. The below given code is used to store and retrieve the data
This code for Storing the data,
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();
}
}
This code for Retrieving the data,
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;
}
Upvotes: 0
Views: 127
Reputation: 562
I would recommend to use memcache, that way the fetch will be faster, and you will have less null objects in return IMO.
Upvotes: 0
Reputation: 8240
The Datastore replicates data across multiple datacenters. This provides a high level of availability for reads and writes, however, most queries are eventually consistent.
Eventual consistency is a consistency model used in distributed computing to achieve high availability that informally guarantees that, if no new updates are made to a given data item, eventually all accesses to that item will return the last updated value.
This is most likely the reason why your query sometimes returns nothing.
I would recommend you to go through Structuring Data for Strong Consistency article.
Upvotes: 1
Reputation: 134
Here is a useful example:
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);
}
Upvotes: 0