You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by hstang <hu...@mxnmedia.com> on 2007/04/14 15:44:37 UTC

MINA and ThreadLocals used in other frameworks

Hi,

I was wondering if anyone has experiences successfully integrating MINA with
other frameworks that depend on ThreadLocals (e.g. Hibernate, Spring
transactions)?  

I looked through some bits and pieces of MINA code and it appears that
there's one thread that manages processing many different sessions, as it
should be.  The problem I see is that other frameworks often assume the
underlying architecture is a one-thread-per-request model and relies on
ThreadLocal for per-thread state, like transactions for example.  So suppose
if you were to use Hibernate during processing in MINA, is it possible MINA
sessions can pick up each other transaction context accidentally?  Do we
have to trust that other frameworks will cleanup the ThreadLocal variables
when it's done with the request?

-- 
View this message in context: http://www.nabble.com/MINA-and-ThreadLocals-used-in-other-frameworks-tf3576237.html#a9993272
Sent from the mina dev mailing list archive at Nabble.com.


Re: MINA and ThreadLocals used in other frameworks

Posted by Urmeli <vj...@gmx.net>.
The problem is: how can the frameworks determine the logical end of the
thread (the point in time when the thread is returned to the pool). In our
project we use HiveMind as the IoC container. HiveMind has the feature to
mark the entry and exit of a processing thread and all thread-bound
components can register a cleanup listener that is notified when the thread
is marked as exited. Works very well for us. I don't know if Spring has
anything similar ...

Mike
-- 
View this message in context: http://www.nabble.com/MINA-and-ThreadLocals-used-in-other-frameworks-tf3576237.html#a10001649
Sent from the mina dev mailing list archive at Nabble.com.


Re: MINA and ThreadLocals used in other frameworks

Posted by Urmeli <vj...@gmx.net>.
Hi Rob,

I don't think that the HiveMind approach is more complex. You basically call
markThreadEntry() before the processing of the business logic starts and
need to call markThreadExit() afterwards. You can easily wrap this in an
IoFilter (the guy that invented HiveMind and Tapestry actually used a
ServletFilter for this purpose).

Is it possible with Spring to have methods ServiceA.DoThis() and
ServiceB.DoThat() been executed in one transaction by using the proxy
objects ? As far as I understood your statement you would need a
Facade-Object with a method DoThisAndThat() that calls both methods. I think
this adds complexity, doesn't it ?

I hope this doesn't get a bit off-topic :-)

Mike

-- 
View this message in context: http://www.nabble.com/MINA-and-ThreadLocals-used-in-other-frameworks-tf3576237.html#a10003793
Sent from the mina dev mailing list archive at Nabble.com.


Re: MINA and ThreadLocals used in other frameworks

Posted by hstang <hu...@mxnmedia.com>.
Can MINA support using worker threads for processing in IoHandler?  For
example, if I had code like this, can MINA handle synchronizing sessions in
another thread that is not controlled by it?

public class DbHandler implements IoHandlerAdapter{
  ExecutorService executor =  Executors.newFixedThreadPool(5);
  
  public void messageReceived( final IoSession session, Object message )
throws Exception
  {
     executor.execute(new Runnable() {
       // get some data from DB
       session.write(data)  <--- thread-safe code???
     });
  }
}


Rob Butler wrote:
> 
> Generally well built frameworks like Spring will add what they need to the
> ThreadLocal upon entrance of a method and then remove them before exiting
> the method.  This works very well as long as the thread is only servicing
> one session.  Since DB interaction, etc could possibly block these
> operations should be done within a worker thread obtained from the thread
> pool.  As long as you have the Spring bits that access the thread local
> after you get a worker thread everything should work fine.
> 
> Rob
> 
> ----- Original Message ----
> From: hstang <hu...@mxnmedia.com>
> To: dev@mina.apache.org
> Sent: Saturday, April 14, 2007 9:44:37 AM
> Subject: MINA and ThreadLocals used in other frameworks
> 
> 
> Hi,
> 
> I was wondering if anyone has experiences successfully integrating MINA
> with
> other frameworks that depend on ThreadLocals (e.g. Hibernate, Spring
> transactions)?  
> 
> I looked through some bits and pieces of MINA code and it appears that
> there's one thread that manages processing many different sessions, as it
> should be.  The problem I see is that other frameworks often assume the
> underlying architecture is a one-thread-per-request model and relies on
> ThreadLocal for per-thread state, like transactions for example.  So
> suppose
> if you were to use Hibernate during processing in MINA, is it possible
> MINA
> sessions can pick up each other transaction context accidentally?  Do we
> have to trust that other frameworks will cleanup the ThreadLocal variables
> when it's done with the request?
> 
> -- 
> View this message in context:
> http://www.nabble.com/MINA-and-ThreadLocals-used-in-other-frameworks-tf3576237.html#a9993272
> Sent from the mina dev mailing list archive at Nabble.com.
> 
> 
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> 
> 

-- 
View this message in context: http://www.nabble.com/MINA-and-ThreadLocals-used-in-other-frameworks-tf3576237.html#a9997075
Sent from the mina dev mailing list archive at Nabble.com.


Re: MINA and ThreadLocals used in other frameworks

Posted by hstang <hu...@mxnmedia.com>.
Assuming that you have that kind of logic in IoHandler, wouldn't this affect
performance?  

Normally output to the client is determined after processing some input.  So
suppose we use a workerpool, like you have mentioned, to do some processing
for us (possibly use Spring to get us some data from the database) and write
the output in the same workerpool thread.  This is what I have done in my
other post.  It's quite possible that flushing the output would occur in the
next selector.select()-blocking-execution cycle, which takes at least 1s
because MINA doesn't have the traffic masks updated properly to do an actual
write to the client.

Am I understanding this properly?

Rob Butler wrote:
> 
> Your making things more complex than they need to be.
> 
> Spring uses proxy objects for transaction management.  The proxy object
> takes care of adding objects to the ThreadLocal when the proxy method is
> entered and removing them before the proxy method is exited.  If you have
> your thread pool before the proxy object and don't do anything strange to
> "release" the thread from within the nested code everything will work
> fine.
> 
> WorkerPool (allocates thread)
>     |
>     Spring proxy
>         --> start txn add stuff to ThreadLocal
>         --> call proxied method to do actual work
>         --> close (commit/rollback) txn, remove stuff from ThreadLocal
>     Exit spring proxy
>     |
> Return Thread to WorkerPool 
> 
> 
> ----- Original Message ----
> From: Urmeli <vj...@gmx.net>
> To: dev@mina.apache.org
> Sent: Sunday, April 15, 2007 8:01:03 AM
> Subject: Re: MINA and ThreadLocals used in other frameworks
> 
> 
> The problem is: how can the frameworks determine the logical end of the
> thread (the point in time when the thread is returned to the pool). In our
> project we use HiveMind as the IoC container. HiveMind has the feature to
> mark the entry and exit of a processing thread and all thread-bound
> components can register a cleanup listener that is notified when the
> thread
> is marked as exited. Works very well for us. I don't know if Spring has
> anything similar ...
> 
> Mike
> -- 
> View this message in context:
> http://www.nabble.com/MINA-and-ThreadLocals-used-in-other-frameworks-tf3576237.html#a10001649
> Sent from the mina dev mailing list archive at Nabble.com.
> 
> 
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> 
> 

-- 
View this message in context: http://www.nabble.com/MINA-and-ThreadLocals-used-in-other-frameworks-tf3576237.html#a10005156
Sent from the mina dev mailing list archive at Nabble.com.