You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-cs@ibatis.apache.org by Zarar Siddiqi <za...@gmail.com> on 2006/08/27 00:54:01 UTC

Weird but reproducable problem which sets HttpContext to null

A couple days ago I posted an exception I was receiving seemingly
randomly when using IBatis:

http://www.mail-archive.com/user-cs@ibatis.apache.org/msg01152.html

I did some more research and got it to reproduce.  Here it is.


THE SETUP

In my Global.asax, I kick-start a background process using the Timer
class.  This Timer class uses a delgate DoTimerBasedStuff() which is
called periodically. The DoTimerBasedStuff() method accesses two
different databases, MySQL and SQL Server.  Both are setup properly
using their MySQLMapper and SQLServerMapper classes.

Aside from a couple of these background threads, the application also
has a web front which accesses both databases using typical .aspx
pages and classes inherited from Page.  Nothing fancy.

THE PROBLEM

If the application is started by going to index.aspx using a browser
(this page doesn't do any db stuff but triggers Global.asax) and no
other actions are taken until the first call of DoTimerBasedStuff()
all is well and both the background process and the web users are
fine.  IBatis does everything right.

BUT if I start the application by going to index.aspx and then
immediately do some work via the web which accesses a database (before
DoTimerBasedStuff() is executed), the DoTimerBasedStuff() method has
trouble accessing the HttpContext and gives the following error:


Exception: WebSessionStore: Could not obtain reference to HttpContext
  at IBatisNet.DataMapper.SessionStore.WebSessionStore.ObtainSessionContext()
  at IBatisNet.DataMapper.SessionStore.WebSessionStore.get_LocalSession()
  at IBatisNet.DataMapper.SqlMapper.QueryForObject(String
  ...

I guess the question that I should be asking is what is the best way
to run a background process/thread in a web application which uses
IBatis functionality.

Thanks in advance.

Re: Weird but reproducable problem which sets HttpContext to null

Posted by Zarar Siddiqi <za...@gmail.com>.
Thanks for the response.  What you're saying makes sense and I
concluded the same but what threw me off was that initially everything
is working fine (i.e.: the app can access the database) but after a
certain point in time, it loses the session.

So what is the best method of making sure a worker thread has access
to an IBatis session?  Here is how I start the thread in the
Global.asax.cs file:

Timer timer = new Timer();
timer.Interval = shipmentCheckInterval;
timer.Elapsed += new ElapsedEventHandler(checkForShipments);
timer.Enabled = true;
timer.Start();

The checkForShipments() method makes the database calls.

How do I make sure that checkForShipments() doesn't lose the IBatis session?

Thanks for your help.

Zarar




On 9/11/06, Gilles Bayon <ib...@gmail.com> wrote:
> The session storage is different depending you are or not in web context.
> It seems your background process is not running in web context (?) but
> the your SqlMapper heve been initialized in web context so it tries to
> retrieve the session in web context and fails.
>
>
> --
> Cheers,
> Gilles
>
> <a href="http://www.amazon.com/gp/registry/6JCP7AORB0LE">Wish List</a>
>

Re: Weird but reproducable problem which sets HttpContext to null

Posted by Gilles Bayon <ib...@gmail.com>.
The session storage is different depending you are or not in web context.
It seems your background process is not running in web context (?) but
the your SqlMapper heve been initialized in web context so it tries to
retrieve the session in web context and fails.


-- 
Cheers,
Gilles

<a href="http://www.amazon.com/gp/registry/6JCP7AORB0LE">Wish List</a>