1

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 ...
Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147

1 Answers1

2

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

Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87
  • Thank you Shay. There sure are a lot of different APIs into Google Datastore! Client API, AppEngine API, etc. Just going by package name, we have com.google.datastore(.v1), com.google.cloud.datastore, and com.google.appengine.api.datastore. – Joshua Fox Oct 20 '16 at 13:08
  • Hello! Clould you please show us the source of this information? – Rodrigo Borba Feb 06 '18 at 13:42
  • @RodrigoBorba this is pretty old info, but still under flex you need to use the Official Google Cloud Libs and not the ones that comes with the AppEngine SDK – Shay Erlichmen Feb 08 '18 at 13:11