Objectify 5.1 Context Not Started Error due to missing ObjectifyFilter
Asked Answered
B

5

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?

Bergson answered 26/10, 2014 at 19:32 Comment(7)
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.Recurvate
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.1Bergson
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.Tribalism
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;Catechism
i have tried this , but no luck, it still throws the same errorBergson
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 worksLimnetic
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.Bretbretagne
R
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 :)

Rebuild answered 1/2, 2016 at 9:4 Comment(0)
M
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>
Monotheism answered 12/11, 2015 at 11:40 Comment(0)
M
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>
Meuse answered 16/3, 2017 at 20:43 Comment(0)
F
-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();
    }
}
Fogarty answered 9/3, 2015 at 12:39 Comment(1)
this is not the recommend way to register objectify service, also every time u call that path, u r registering every timeBergson
P
-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!

Pentobarbital answered 1/9, 2015 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.