问题
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