Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Google App Engine (java) service classes Thread-Safe?

Is it okay to obtain a reference to a service from factory once and use it for handling multiple requests? It can be best explained using following pseudo-code for a servlet:

SomeServlet...{
  MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();
  UserService userService = UserServiceFactory.getUserService();
  DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();

  doGet(...){
    userService.doSomething(...);
  }
...
}

It may be a premature optimization but i am just curious to know what is the cost of obtaining service for each request. Please share your insight.

like image 907
kdabir Avatar asked Feb 26 '11 16:02

kdabir


1 Answers

In the google group thread http://groups.google.com/group/google-appengine-java/browse_thread/thread/d3f1536084f59c22, Ikai Lan (from the GAE team at Google) says MemcacheService is thread-safe, but that caching it is not useful, because there is just one object allocation each time you get the service from the factory.

Since all the services are obtained in a similar way, I think we could assume that they all follow the same design and are all thread-safe. But since it isn't costly to get a new instance at each query, I wouldn't cache them.

like image 176
JB Nizet Avatar answered Nov 04 '22 16:11

JB Nizet