GAE,本地数据存储区不会创建

时间:2014-01-27 11:08:25

标签: google-app-engine jpa intellij-idea google-cloud-datastore

我不知道GAE在多大程度上不容易理解:(

我的servlet操纵一个json字符串,然后我试图将它存储在数据存储区中。 当我运行应用程序时,我得到了这个输出:

Jan 27, 2014 6:59:04 PM com.google.appengine.api.datastore.dev.LocalDatastoreService load
INFO: The backing store, D:\Android\IntelliJ IDEA\workspace\EyeBall\AppEngine\out\artifacts\AppEngine_war_exploded\WEB-INF\appengine-generated\local_db.bin, does not exist. It will be created.
1
2
3
4
5
7
***
***
***
***
***
8
9

虽然提到将创建local_db.bin,但是当我导航到该目录时,该文件不存在。此外,当我在浏览器中打开http://localhost:8080/_ah/admin/datastore时,实体种类下拉列表中没有显示任何内容。

所以wtf发生在local_db.bin上吗?为什么它不生成? 任何建议将不胜感激。感谢。

================== 的更新: 我根据请求添加了我的代码。

    private static final String NO_DEVICE_ID = "FFFF0000";
    private static final String SAMPLE_JSON = "{\"history\":[{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com\",\"url\":\"https://www.maybank2u.com.my/mbb/Mobile/info.do\",\"visits\":14},{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com\",\"url\":\"https://www.maybank2u.com.my/mbb/Mobile/adaptInfo.do\",\"visits\":4},{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com\",\"url\":\"http://www.maybank2u.com.my/mbb_info/m2u/public/personalBanking.do\",\"visits\":16},{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com Online Financial Services\",\"url\":\"https://www.maybank2u.com.my/mbb/m2u/common/M2ULogin.do?action=Login\",\"visits\":52},{\"date\":null,\"info\":null,\"title\":\"‭BBC\",\"url\":\"http://www.bbc.co.uk/persian/\",\"visits\":16}]}";

    private static final String QUERY_HISTORY_DEVICE = "SELECT m FROM HistoryDeviceJPA m WHERE m.userUUID = :keyword ORDER BY m.domain ASC";
    private static final String QUERY_HISTORY        = "SELECT m FROM HistoryJPA m WHERE m.pageAddress = :keyword";

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        displayError(response, "The page doesn't support httpGet");
        String deviceId = NO_DEVICE_ID;
        String content = SAMPLE_JSON;
        System.out.println("1");
        HistoryBrowser historyBrowser = parseJson(content);
        if(historyBrowser == null)
            return;
        System.out.println("2");
        List<HistoryBrowser.BrowserInfo> historyList = historyBrowser.getHistory();
        if(historyList == null)
            return;

        System.out.println("3");

        List<HistoryDeviceJPA> historyDeviceJPAList = new ArrayList<HistoryDeviceJPA>(historyList.size());
        for(int i=0; i<historyList.size(); i++) {
            try {
                HistoryBrowser.BrowserInfo browser = historyList.get(i);
                HistoryDeviceJPA historyDeviceJPA = new HistoryDeviceJPA();
                historyDeviceJPA.setUserUUID(deviceId);
                historyDeviceJPA.setDomain(getDomainName(browser.getUrl()));
                historyDeviceJPA.setPageAddress(browser.getUrl());
                historyDeviceJPA.setPageTitle(browser.getTitle());
                historyDeviceJPA.setPageVisits(browser.getVisits());

                historyDeviceJPAList.add(historyDeviceJPA);

            } catch (URISyntaxException e) {
                System.out.println(e.getMessage());
            }
        }
        System.out.println("4");

        // get history of device from data store
        EntityManager em = EMF.get().createEntityManager();
        Query q = em.createQuery(QUERY_HISTORY_DEVICE).setParameter("keyword", deviceId);
        @SuppressWarnings("unchecked")
        List<HistoryDeviceJPA> dbList = (List<HistoryDeviceJPA>) q.getResultList();

        System.out.println("5");

        // If there is no result (shows there is no record for that device)
        if(dbList == null)
            addHistoryDeviceJPAToDs(historyDeviceJPAList);
        else {
            System.out.println("7");
            // find each item in datastore and replace them if needed
            // if current page visit is less ot equal than previous visit don't do anything (remove item form historyDeviceJPAList)
            outerLoop:
            for(int i=0; i<historyDeviceJPAList.size(); i++) {
                HistoryDeviceJPA deviceItem = historyDeviceJPAList.get(i);
                System.out.println("***");
                for(int j=0; j<dbList.size(); j++) {
                    HistoryDeviceJPA dbItem = dbList.get(j);

                    if(deviceItem.getPageAddress().equalsIgnoreCase(dbItem.getPageAddress())) {
                        if(deviceItem.getPageVisits() > dbItem.getPageVisits()) {
                            long diff = deviceItem.getPageVisits() - dbItem.getPageVisits();
                            dbItem.setPageVisits(deviceItem.getPageVisits());

                            HistoryJPA historyJPA = findHistoryJPA(dbItem.getPageAddress());
                            historyJPA.setPageVisits(historyJPA.getPageVisits() + diff);

                            // update datastore
                            addHistoryDeviceJPAToDs(dbItem);
                            addHistoryJPAToDs(historyJPA);

                            // don't check other items of j list
                            break outerLoop;
                        }
                    }
                }
            }
            System.out.println("8");
        }
        System.out.println("9");
        // http://www.sohailaziz.com/2012/06/scheduling-activities-services-and.html
        // https://dev.twitter.com/docs/api/1.1
        // https://developers.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata?csw=1#Updating_an_Object
        // http://en.wikibooks.org/wiki/Java_Persistence/Inheritance
    }

和6在这里:

private void addHistoryDeviceJPAToDs(List<HistoryDeviceJPA> list) {
        System.out.println("6");
        EntityManager em = EMF.get().createEntityManager();
        try {
            for (int i=0; i<list.size(); i++) {
                System.out.println("=> " + i + " - " + list.get(i).toString());
                em.getTransaction().begin();
                em.persist(list.get(i));
                em.getTransaction().commit();
            }
        } finally {
            em.close();
        }
    }

enter image description here

2 个答案:

答案 0 :(得分:0)

调试后我发现问题出在这一行:

List<HistoryDeviceJPA> dbList = (List<HistoryDeviceJPA>) q.getResultList();
        if(dbList == null)
            addHistoryDeviceJPAToDs(historyDeviceJPAList);

'dbList'永远不会为null,如果数据存储区中没有任何内容,则它的大小为0。这就是为什么addHistoryDeviceJPAToDs方法永远不会被调用的原因。通过将代码更改为以下问题解决并创建本地数据库。

List<HistoryDeviceJPA> dbList = (List<HistoryDeviceJPA>) q.getResultList();
        if(dbList == null)
            return;

        System.out.println("5");

        // If there is no result (shows there is no record for that device)
        if(dbList.size() == 0)
            addHistoryDeviceJPAToDs(historyDeviceJPAList);

答案 1 :(得分:0)

对于遇到同一问题的其他人 -

在将数据放入数据存储区之前,GAE不会创建local_db.bin。因此,如果文件不存在,则应用程序代码中可能存在错误。

相关问题