David,
I have implemented a DataBase Manager, that saves all the objects
registered with it.
So the client can just register all the objects it wants to save,Delete
with this Manager, and then flushes the manager (much like a O/R Session)
I would like to create a transaction object and add it to the call
context or current thread, then all the database operations can check
whether there is a transaction object in the call context, this would
save me from passing the transaction object reference around, and make
the code much cleaner.
I can use [ThreadStatic] attribute (or add it to the TLS), but threads
gets reused,
on another note Is a thread dedicated to the whole life time of a
request, or does it interleaves between multiple requests ?
anyways any comments would be appreciated.
TIA
-ashish
Brian Gideon wrote:
> Ashish,
>
[quoted text clipped - 30 lines]
>>TIA
>>-ashish
>>Hi All,
>>I am designing a class library that can be used by web and windows client
[quoted text clipped - 17 lines]
>
> David
David Browne - 10 Nov 2005 03:20 GMT
> David,
>
[quoted text clipped - 14 lines]
> on another note Is a thread dedicated to the whole life time of a request,
> or does it interleaves between multiple requests ?
ThreadStatic is perfect for this. A thread will be assigned to your
request, and the same thread will handle the whole request. In an outer
scope you will need to establish the call context and save it into a
ThreadStatic variable. Then call into whatever code you want to service
with the call context, and on return commit the transaction, and on any
exception, roll back. Something like
class DBContext
{
[ThreadStatic]
public static DBContext CurrentContext;
. . .
public readonly SqlConnection Connection;
. . .
}
. . .
DBContext.CurrentContext = new DBContext(...);
try
{
//do whatever
DBContext.CurrentContext.Commit();
}
catch (Exception ex)
{
DBContext.CurrentContext.Rollback();
throw ex;
}
finally
{
DBContext.CurrentContext.Connection.Close();
DBContext.CurrentContext = null;
}
Then anywhere in the call stack of the invoked components you can access the
CurrentContext's Connection with
DBContext.CurrentContext.Connection
David