persistence/play-java-jpa/src/main/java/play/db/jpa/JPAEntityManagerContext.java
@SuppressWarnings("unchecked")
  public Deque<EntityManager> emStack(boolean threadLocalFallback) {
    return Http.Context.safeCurrent()
        .map(
            context -> {
              Object emsObject = context.args.get(CURRENT_ENTITY_MANAGER);
              if (emsObject != null) {
                return (Deque<EntityManager>) emsObject;
              } else {
                Deque<EntityManager> ems = new ArrayDeque<>();
                context.args.put(CURRENT_ENTITY_MANAGER, ems);
                return ems;
              }
            })
        .orElseGet(
            () -> {
              // Not a web request
              if (threadLocalFallback) {
                return this.get();
              } else {
                throw new RuntimeException(
                    "No Http.Context is present. If you want to invoke this method outside of a HTTP request, you need to wrap the call with JPA.withTransaction instead.");
              }
            });
  }
@SuppressWarnings("unchecked")
  public Deque<EntityManager> emStack(boolean threadLocalFallback) {
    return Http.Context.safeCurrent()
        .map(
            context -> {
              Object emsObject = context.args.get(CURRENT_ENTITY_MANAGER);
              if (emsObject != null) {
                return (Deque<EntityManager>) emsObject;
              } else {
                Deque<EntityManager> ems = new ArrayDeque<>();
                context.args.put(CURRENT_ENTITY_MANAGER, ems);
                return ems;
              }
            })
        .orElseGet(
            () -> {
              // Not a web request
              if (threadLocalFallback) {
                return this.get();
              } else {
                throw new RuntimeException(
                    "No Http.Context is present. If you want to invoke this method outside of a HTTP request, you need to wrap the call with JPA.withTransaction instead.");
              }
            });
  }
Get the EntityManager stack.

@param threadLocalFallback if true, fall back to a ThreadLocal queue of entity managers if no
HTTP.Context object is found.
@return the queue of entity managers.