views:

11

answers:

1

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.

A: 

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 corresponding destroy() method if you need to do any cleanup. See here and here.

Taylor Leese