I have a code snippet:
private static DatastoreService _db;
public static DatastoreService db() throws IOException
{
if(_db==null) _db = DatastoreServiceFactory.getDatastoreService();
return _db;
}
Is this safe approach? I cached the DatastoreService object statically and re-used over a single servlet execution. However, I have no sure about the servlet disposes after a request.
The best place for that kind of initialization code would be in your
init()method that you should override in your servlet. There is also a correspondingdestroy()method if you need to do any cleanup. See here and here.