Class ObjectifyService


  • public class ObjectifyService
    extends Object
    Holder of the master ObjectifyFactory and provider of the current thread-local Objectify instance. Call ofy() at any point to get the current Objectify with the correct transaction context.
    Author:
    Jeff Schnitzer
    • Constructor Detail

      • ObjectifyService

        public ObjectifyService()
    • Method Detail

      • init

        public static void init()
        This is a shortcut for ObjectifyService.init(new ObjectifyFactory())
      • factory

        public static ObjectifyFactory factory()
        Returns:
        the current factory
      • ofy

        public static Objectify ofy()
        The method to call at any time to get the current Objectify, which may change depending on txn context
      • run

        public static <R> R run​(Work<R> work)

        Runs one unit of work, making the root Objectify context available. This does not start a transaction, but it makes the static ofy() method return an appropriate object.

        Normally you do not need to use this method. When servicing a normal request, the ObjectifyFilter will run this for you. This method is useful for using Objectify outside of a normal request - using the remote api, for example.

        Alternatively, you can use the begin() method and close the session manually.

        Returns:
        the result of the work.
      • begin

        public static Closeable begin()

        An alternative to run() which is somewhat easier to use with testing (ie, @Before and @After) frameworks. You must close the return value at the end of the request in a finally block. It's better/safer to use run().

        This method is not typically necessary - in a normal request, the ObjectifyFilter takes care of this housekeeping for you. However, in unit tests or remote API calls it can be useful.