How do I register API environment for Datastore in

2019-06-11 14:50发布

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条回答
聊天终结者
2楼-- · 2019-06-11 15:17

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

查看更多
登录 后发表回答