-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Calum,

On 6/20/2011 6:12 AM, Calum wrote:
> On 15 June 2011 18:54, Konstantin Kolinko <knst.koli...@gmail.com> wrote:
>> The request and response objects must never be accessed outside the
>> request processing cycle.  The objects are recycled and cleared or
>> reused for subsequent request and responses, and are not guaranteed to
>> be thread-safe.  The consequences can be severe.
> 
> I was interested by this.
> So:
> 
>     private Service service;
> 
>     @Override
>     public void init() throws ServletException {
>         super.init();
>         service = (Service)
> WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getBean("service");
>     }
> 
>     protected void processRequest(HttpServletRequest request,
> HttpServletResponse response)
>             throws ServletException, IOException {
>     service.logHit(request);
>     ....
>     }
> 
> 
> is a bad thing?

That depends on what Service.logHit does. If it only uses the
HttpServletRequest object during the method's lifetime, then everything
is fine. If it retains a reference to the request object, you will
probably end up with a problem.

> Could you explain why?

Tomcat re-uses the same request and response objects for a long time,
and so multiple request processor threads will see the same object over
the life of the server. If any of that code retains a reference to the
request, by the time the object is re-referenced by the same code for
whatever reason, the object will have "moved on" and will almost
certainly be invalid (from the perspective of the code that cached it)
and might /actually/ be invalid (because it is between requests and
Tomcat has actively invalidated it).

> I would assume that a new request object is created each time, and
> that while something has a reference to it, it will be fine, and when
> logHit has finished with it, it will be eligible for garbage
> collection.

Nope: see Konstantin's and me comments above.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEUEARECAAYFAk3/bwUACgkQ9CaO5/Lv0PBaLQCWLMYh41usETP5XWpEvGWF+Y1d
mwCfSA9l2qR27DGT+tKi7MgrwUwKowc=
=Sgfb
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to