سؤال

I've been assuming that static initialization (e.g. for my persistence library) will persist between requests for a given instance? Suddenly it occurred to me that maybe I'm wrong - maybe my app's init is redone for each request even on a warm instance.

Here's why I'm asking:

I have a request handler (it happens to be a Google Endpoint, but I don't think that is relevant) that receives a list of entities and saves them.

The entities can be any of 20 different types, so my static initialization 'registers' all 20 different entity types. I happen to be using objectify, so it looks like this:

@Api(name = "myendpoint")
public class MyEndpoint {
  static {
    ObjectifyService.register( EntityOne.class );
    ObjectifyService.register( EntityTwo.class );
    ... x20
  }

If it is doing all this 'registering' for each request then I had better change this to only register for the entities needed for the request.

(On the other hand, if I'm right and my static init is only done when creating a new instance then I should put as much initialization as I can into the static init.)

هل كانت مفيدة؟

المحلول

Static initializer blocks are run once when the class is being loaded.

نصائح أخرى

Yes, static initialization blocks are only run when the class is being loaded. That means that it happens only once per GAE instance.

Something else worth considering: It is often better to register these classes with Ofy via your own Objectify service class, and then use that to access Objectify functionality. See https://code.google.com/p/objectify-appengine/wiki/BestPractices ... That ensures that the blocks are run before any datastore access happens.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top