18

with the latest version of Objectify (5.1), am getting following error when i try to access the ofy() method

You have not started an Objectify context. You are probably missing the ObjectifyFilter. If you are not running in the context of an http request, see the ObjectifyService.run() method.

i am running it from appengine web application, same code and configuration worked fine in older versions

following is my configuration, similar to the example provided in objectify documentation

web.xml

<filter>
    <filter-name>ObjectifyFilter</filter-name>
    <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ObjectifyFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

OfyService class

public class OfyService {

static {
    long start = System.currentTimeMillis();

    factory().register(User.class);

    log.info(" Entity Registration took : {} ms", (System.currentTimeMillis() - start));
}

public static Objectify ofy() {
    return ObjectifyService.ofy();
}

public static ObjectifyFactory factory() {
    return (ObjectifyFactory) ObjectifyService.factory();
}
}

but i do defined ObjectifyFilter , any idea why am i getting this error? and how can i fix it?

Thanks!

UPDATE:

I have updated the objectify version to v5.1.5 but still the issue is not resolved any update on this?

Ramesh Lingappa
  • 2,448
  • 20
  • 33
  • Check if you call ofy() at startup e.g. for some reason - out of the scope of a request - then you have not gone through the ObjectifyFilter. – koma Oct 26 '14 at 20:57
  • No its not called anywhere else, i strictly call ofy() through OfyService class, also FYI the same code works fine in version prior to 5.1 – Ramesh Lingappa Oct 27 '14 at 08:47
  • 1
    Check to make sure that 1) you aren't calling ofy() before the filter is installed; say, perhaps, from another filter higher in the chain and 2) that you don't have multiple versions of objectify on your classpath (mvn clean). You should be able to look at the stacktrace and see what code is trying to use objectify outside of the filter; if the filter was installed, you would see it in the stacktrace. Post it. – stickfigure Oct 27 '14 at 16:27
  • Does your code run as a task in the TaskQueue? Mine did so I had to add `Closeable closeable = begin();` before and `closeable.close();` after to avoid the error you got. Note that begin() comes from doing `import static com.googlecode.objectify.ObjectifyService.begin;` – Michael Osofsky Nov 07 '14 at 02:33
  • i have tried this , but no luck, it still throws the same error – Ramesh Lingappa Mar 11 '15 at 11:04
  • Had the same problem here. In my case it was only the warm up request, which throws the exception. Found no documentation about that, but the warm up request "/_ah/warmup" does not respect any of my filters (including the objectify filter). I put all my warm up code in a ObjectifyService.run() and now it works – Zensursula Aug 26 '15 at 12:33
  • In eclipse (Kepler) I am using two GAE modules, both of which use Objectify. For the second module I get this error when running in the GAE server in eclipse (even with the filter present on the second module).. but when I release the code it runs correctly. – andrew pate Sep 22 '16 at 12:50

5 Answers5

1

make sure your StartupActions class looks like:

@Start(order=100)
public void generateDummyDataWhenInTest() {
    if (ninjaProperties.isDev()) {
        try (Closeable closeable = ObjectifyService.begin()) {
            ObjectifyProvider.setup();
        } catch (IOException ex) {
            Logger.getLogger(StartupActions.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
}

We should have fixed that long ago in the archetype, but it's not really high on the prio list. It would be awesome if you could push a PR :)

Ra_
  • 569
  • 5
  • 13
0

I just started to use Objectify and had the same problem than you. I just don't read the setup information... Simply add that on my web.xml and it worked:

<filter>
    <filter-name>ObjectifyFilter</filter-name>
    <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ObjectifyFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Phil
  • 4,730
  • 1
  • 41
  • 39
0

I was facing the same error and this solusion worked for me

add in web.xml

   <filter>
    <filter-name>ObjectifyFilter</filter-name>
    <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ObjectifyFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>
humazed
  • 74,687
  • 32
  • 99
  • 138
-1
import static com.googlecode.objectify.ObjectifyService.ofy;

import java.util.List;
...
import ar.com.cra.entity.State;
import com.googlecode.objectify.ObjectifyService;

@Controller
@RequestMapping("/state")
public class StateController {

    @RequestMapping(value = "/addState", method = RequestMethod.GET)
    public String getAddState(ModelMap model) {
        return "state/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView add(HttpServletRequest request, ModelMap model) {

        ObjectifyService.register(State.class);

        State state;
        try {
            state = new State();
            state.setName(request.getParameter("name"));
            state.setShortName(request.getParameter("shortName"));
            ofy().save().entity(state).now();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ModelAndView("redirect:list");

    }

UPDATE

public class DatastoreService {

    static {
        factory().register(State.class);
    }

    public static Objectify ofy() {
        return ObjectifyService.ofy();
    }
}
Guillermo81
  • 189
  • 11
-1

I have been having the same problem. I found what caused the problem (for me). I have been using Guice and sitebricks to build my back end services.

Problem

In order to make use of Objectify I had to bind ObjectifyFilter as Singleton in one of my modules. The module that I was performing the binding extends a ServletModule which is installed via Guice in SitebricksModule where I bind my services to urls. The SiteBricksModule is then passed to Guice's injector.

I observed that when I installed one module in another the problem persisted.

Solutions that I found

  • I separated the two modules and passed them both to Guices's injector.

or

  • Separate Objectify stuff like binding the filter and specifying filter pattern in a different module and pass that first to Guice's injector.

Hope this helps!

NewestStackOverflowUser
  • 2,792
  • 5
  • 22
  • 31