GAE, Local datastore does not create

时光毁灭记忆、已成空白 提交于 2019-12-25 02:56:12

问题


I have no idea in what extend GAE is not easy to understand :(

My servlet manipulate a json string and then I'm trying to store it in datastore. When I run the application I'm getting this output:

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

Although it's mentioned that local_db.bin will be created but when I navigate to that directory the file is not there. Also, when I open http://localhost:8080/_ah/admin/datastore in browser nothing displays in Entity Kind drop down list.

So wtf happene to local_db.bin? Why it doesn't generates? any suggestion would be appreciated. thanks.

================== UPDATE: I added my code based on request.

    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
    }

and 6 is here:

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();
        }
    }


回答1:


after debug I found the problem is in this line:

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

'dbList' is never null and it's size is 0 if there is nothing in datastore. That's why addHistoryDeviceJPAToDs method never invoked. By changing the code to following problem solved and local db created.

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);



回答2:


For other people who come across the same issue --

GAE will not create local_db.bin until you put data in the datastore. So if the file is not there, there is likely a bug in the application code.



来源:https://stackoverflow.com/questions/21379265/gae-local-datastore-does-not-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!