2012-11-08 88 views
0

我有一個servelet,它可以從rest服務或jsp表單發佈請求,這兩個請求都將調用內部方法(internalAddPodcast())將實體添加到數據存儲。datatore.put()不添加到數據庫?

當我從jsp頁面點擊internalAddPodcast()時,它工作正常,我可以看到添加後立即通過查詢添加成功添加實體。但是當我從其餘方法addPodcast()中執行時,datastore.put()似乎實際上並沒有添加到數據存儲中,因爲我在put()之後嘗試並檢索它,並且沒有任何內容返回。向下看這個類的底部,我把評論「/ /這個查詢是空的,當從休息服務添加:(這是我期望有一些結果回來,特別是我剛剛放在實體數據存儲。

package com.aol.sharepodder; 

import java.io.IOException; 
import java.util.Date; 
import java.util.List; 
import java.util.logging.Logger; 

import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.DefaultValue; 
import javax.ws.rs.FormParam; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 

import com.google.appengine.api.datastore.DatastoreService; 
import com.google.appengine.api.datastore.DatastoreServiceFactory; 
import com.google.appengine.api.datastore.Entity; 
import com.google.appengine.api.datastore.FetchOptions; 
import com.google.appengine.api.datastore.KeyFactory; 
import com.google.appengine.api.datastore.PreparedQuery; 
import com.google.appengine.api.datastore.Query; 
import com.google.appengine.api.users.User; 
import com.google.appengine.api.users.UserService; 
import com.google.appengine.api.users.UserServiceFactory; 

@Path("/add/podcast/") 
public class AddPodcastServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 
    private static final Logger log = Logger.getLogger(AddPodcastServlet.class 
      .getName()); 

    public void doPost(HttpServletRequest req, HttpServletResponse resp) 
      throws IOException { 

     String email = req.getParameter("email"); 
     String collectionName = req.getParameter("collectionName"); 
     String podcast_url = req.getParameter("podcast_url"); 
     String podcast_description = req.getParameter("podcast_description"); 
     String podcast_title = req.getParameter("podcast_title"); 

     log.info("--post adding " + collectionName); 

     internalAddPodcast(email, collectionName, podcast_url, 
       podcast_description, podcast_title); 

     resp.sendRedirect("/collection_details.jsp?collectionName=" 
       + collectionName + "&email=" + email); 
    } 

    @POST 
    @Produces("text/plain") 
    @Consumes("application/x-www-form-urlencoded") 
    public String addPodcast(
      @DefaultValue("[email protected]") @FormParam("email") String email, 
      @DefaultValue("default") @FormParam("collectionName") String collectionName, 
      @DefaultValue("") @FormParam("podcast_url") String podcast_url, 
      @DefaultValue("") @FormParam("podcast_description") String podcast_description, 
      @DefaultValue("") @FormParam("podcast_title") String podcast_title) { 
     try { 
      internalAddPodcast(email, collectionName, podcast_url, 
        podcast_description, podcast_title); 
      if (podcast_url == "") { 
       return "No url supplied"; 
      } 
      return "true"; 
     } catch (Exception e) { 
      return e.getMessage(); 
     } 
    } 

    private void internalAddPodcast(String email, String collectionName, 
      String podcast_url, String podcast_description, String podcast_title) { 
     log.info("--INTERNAL ADD "); 
     log.info("--email " + email); 
     log.info("--collectionName " + collectionName); 
     log.info("--podcast_url " + podcast_url); 
     log.info("--podcast_description " + podcast_description); 
     log.info("--podcast_title " + podcast_title); 

     UserService userService = UserServiceFactory.getUserService(); 
     User user = userService.getCurrentUser(); 

     Entity podcast = new Entity("Podcast"); 
     podcast.setProperty("collectionName", collectionName); 
     podcast.setProperty("user", user); 
     podcast.setProperty("email", email); 
     Date date = new Date(); 
     podcast.setProperty("date", date); 
     podcast.setProperty("podcast_title", podcast_title); 
     podcast.setProperty("podcast_description", podcast_description); 
     podcast.setProperty("podcast_url", podcast_url); 

     DatastoreService datastore = DatastoreServiceFactory 
       .getDatastoreService(); 
     datastore.put(podcast); 

     //try to log the podcast that I just got done adding to the datastore 
     Query query = new Query("Podcast"); 
     PreparedQuery pq = datastore.prepare(query); 

      //THIS QUERY IS EMPTY WHEN ADDED FROM THE REST SERVICE :(
     for (Entity p : pq.asIterable()) { 
      log.info("_loop " + " - " + KeyFactory.keyToString(p.getKey()) 
        + " -- " + p.getProperty("podcast_title") + " - " 
        + p.getProperty("podcast_url")); 
     } 
    } 
} 

任何想法,我做錯了什麼,爲什麼我試圖從其他方法添加的實體沒有被添加到該數據存儲。

我知道,在這兩種情況下,(無論是從jsp後,或其他服務),當我到達internalAddPodcast()所有的方法參數進入正確

回答

1

啊哈!我找到了。我沒有記錄正在拋出的異常。基本上我試圖存儲一個超過500個字符的字符串屬性,它拋出了一個我需要注意的異常:)因此,它永遠不會到達datastore.put()

+3

這就是爲什麼你永遠不應該在你的代碼周圍有'try/catch(Exception e)'。 –

+0

同意。謝謝Nick。 –

2

High Re摺疊數據存儲最終是一致的。這意味着大多數查詢都不能保證反映剛剛對數據存儲進行的更改 - 包括返回剛剛插入的記錄。閱讀關於此的更多信息以及如何執行強烈一致的查詢here

+0

感謝您的評論!如上所述,我發現了這個問題。 :)我也提高了你的評論:) –