cayenne-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Gentry <mgen...@masslight.net>
Subject Re: Stateless RequestHandler
Date Mon, 10 Dec 2012 13:31:18 GMT
Hi John,

If you'd like (and wouldn't mind), I could wrap that up as one of the
examples in the book I'm putting together.

mrg



On Fri, Dec 7, 2012 at 3:22 PM, John Huss <johnthuss@gmail.com> wrote:

> In Cayenne, there is the SessionContextRequestHandler which creates a
> session and binds an ObjectContext to it.  Most of my apps are
> *stateless*(no sessions) so I don't use this but instead I have a very
> simple request
> handler that creates a new ObjectContext for each request.  Is this
> something worth adding to the project?
>
> import javax.servlet.ServletRequest;
> import javax.servlet.ServletResponse;
>
> import org.apache.cayenne.BaseContext;
> import org.apache.cayenne.ObjectContext;
> import org.apache.cayenne.configuration.CayenneRuntime;
> import org.apache.cayenne.configuration.server.ServerRuntime;
> import org.apache.cayenne.configuration.web.RequestHandler;
> import org.apache.cayenne.configuration.web.WebUtil;
> import org.apache.cayenne.di.Inject;
> import org.apache.cayenne.di.Injector;
>
> /**
>  * Stateless implementation of the {@link RequestHandler} that creates a
> new
>  * {@link ObjectContext} for each request and binds it to the request
> thread.
>  *
>  * @since 3.2
>  */
> public class StatelessContextRequestHandler implements RequestHandler {
>
>     // using injector to lookup services instead of injecting them directly
> for lazy
>     // startup and "late binding"
>     @Inject
>     private Injector injector;
>
> @Override
> public void requestStart(ServletRequest request, ServletResponse response)
> {
>         CayenneRuntime.bindThreadInjector(injector);
> ServerRuntime runtime = (ServerRuntime)
> WebUtil.getCayenneRuntime(request.getServletContext());
> ObjectContext context = runtime.getContext();
> BaseContext.bindThreadObjectContext(context);
> }
>  @Override
> public void requestEnd(ServletRequest request, ServletResponse response) {
> CayenneRuntime.bindThreadInjector(null);
> BaseContext.bindThreadObjectContext(null);
> }
> }
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message