How do I register API environment for Datastore in GAE Flexible Environment?

纵然是瞬间 提交于 2019-12-08 09:50:44

问题


When trying to run Datastore in a Servlet in Google App Engine Flexible Environment I get "No API environment is registered for this thread." (Details below.)

I see questions on StackOverflow about this happening in local machines or unit tests, but this is happening in GAE in a regular Servlet.

Here is the entirety of my Datastore code, a simple query. Do I need to register an API environment? If so, how?

List<String> kinds = new ArrayList<String>();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(DatastoreServiceConfig.Builder                         .withImplicitTransactionManagementPolicy(ImplicitTransactionManagementPolicy.NONE));

PreparedQuery global = datastore.prepare(new Query("__kind__"));
for (Entity globalStat : global.asIterable()) {
   String kindName = globalStat.getKey().getName();
   kinds.add(kindName);
}

Stacktrace

java.lang.NullPointerException: No API environment is registered for this thread.
    at com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppId(DatastoreApiHelper.java:180)
    at com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppIdNamespace(DatastoreApiHelper.java:196)
    at com.google.appengine.api.datastore.Query.<init>(Query.java:208)
    at com.google.appengine.api.datastore.Query.<init>(Query.java:139)
    at ...

回答1:


In order to use the datastore under Flexible environment you need to use the Cloud Datastore
You can use the Compat enviroment if you want to use the datastore using "old" APIs



来源:https://stackoverflow.com/questions/40147999/how-do-i-register-api-environment-for-datastore-in-gae-flexible-environment

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