On 8/30/07, Chris Winters <[EMAIL PROTECTED]> wrote:
>
>
>
> Maarten Bosteels-4 wrote:
> >
> > Of course, your code should not assume that two messageReceived events
> for
> > the same IoSession will be handled by the same thread.
> > So storing something in a threadlocal in the first messageReceived event
> > and
> > trying to retrieve it in the second event is NOT safe.
> > (at least not in combination with an executorfilter)
> > But that's rather obvious, I guess.
> >
>
> The various ThreadLocals only come into play once the message has already
> been received in its entirety and is being processed by the application--
> in
> fact, I put the ExecutorFilter AFTER the protocol filter to ensure this.
> Sound ok?


yep. It's also ok if there would be an ExecutorFilter before the
ProtocolFilter.
Just make sure that the usage of the ThreadLocal is within one mina event.

Take for example Spring's transaction handling: if your IoHandler calls
MyService.doSomething
and you let Spring wrap a database transaction around this method, then
Spring would use a ThreadLocal
to store the java.sql.Connection and the TransactionStatus to be used
throughout the method.
That's ok, because the usage of the ThreadLocal is confined to one mina
event
(the transaction will be committed (or rolled back) before messageReceived
returns) .

But suppose looks like this
* you start a database transaction from within messageReceived
* store the Connection and TransactoionStatus in a ThreadLocal
* in messageSent you retrieve the Connection from the ThreadLocal to commit
the transaction.

That would not be ok (and actually be a very bad design).

Actually, using MINA is not so much different from writing servlets (or
spring controllers):
* you should not store conversational state in objects that are shared
between threads (eg Servlets, Service objects, ...)
   (store this info in the IoSession)
* usage of Thread locals should last longer than one event (most frameworks
will do this properly)

I hope it's clear now ?

Maarten

Chris
>
> --
> View this message in context:
> http://www.nabble.com/ThreadLocal-with-Mina%3A-ok-or-not--tf4354098s16868.html#a12410308
> Sent from the Apache MINA Support Forum mailing list archive at Nabble.com
> .
>
>

Reply via email to